Angular Content Projection Explained: Complete Guide

Content projection is a technique where you project content that you want to use in another component. For example, you may have a product card component that accepts the contents of another component such as product name, product description, etc.

The various angular content projection types and their implementations are listed below:

Single-slot Content Projection

Single-slot content projection is the simplest type of content projection. Making a component into which you can project just single content is referred to as single-slot content projection.

🔸 The steps for developing single-slot content projection are as follows:

Step 1 – Create a component

Step 2 – Add an <ng-content> element where you want the projected content to appear

import { Component } from '@angular/core';

@Component({
selector: 'app-single-slot-projection',
template: `
<h2>Single-slot content projection</h2>
<ng-content></ng-content>
`
})
export class SingleSlotProjectionComponent{}

Step 3 – Call <app-single-slot-projection> from <app.component.html> and pass the h1 tag with any message, Wherever you have included “ng-content” in “app-single-slot-projection,” the message will be displayed.

<app-single-slot-projection>
<h1>Single slot projection working</h1>
</app-single-slot-projection>

An actual DOM element is not created by the placeholder element “ng-content”. Applied custom attributes to “ng-content” are disregarded.

Related read: React Virtual DOM vs Real DOM

Unlock the Potential of AngularJS: Hire an Expert AngularJS Developer

Multi-slot Content Projection

What if you want to use your component for content projection, but you have the requirement to project the content in different places, for this, you can use multi-slot content projection.

You can utilize multi-slot content projection if you want to use your component for content projection but you need to project the material in multiple locations.

Several slots can be found in a component. A CSS selector can be specified for each slot to control what content is placed there. Multi-slot content projection is the name of this pattern. You need to specify where you want the projected material to appear when using this pattern. The choose attribute of ng-content> is used to complete this task.

The term “multi-slot content projection” is used to describe this pattern. You must indicate the location of the projected content while using this pattern.

🔸 The steps for developing multi-slot content projection are as follows:

Step 1 – Create a component for multi-slot projection by using the command ng g c multi-slot-projection.

Step 2 – The bootstrap library is what we’re using in this example, but you can use whatever UI framework you choose,
For installing bootstrap use the command npm i @ng-bootstrap/ng-bootstrap.

Step 3 – Open the multi-slot-projection-component.html file and paste the following code.

<div class="alert alert-primary" role="alert">
<ng-content select="[description]"></ng-content>
</div>
<ul class="list-group shadow">
<li class="list-group-item active" aria-current="true">
<ng-content select="[title]"></ng-content>
</li>
<li class="list-group-item">A second item</li>
<li class="list-group-item">A third item</li>
<li class="list-group-item">A fourth item</li>
<li class="list-group-item">And a fifth one</li>
</ul>

Element that uses the description attribute is projected into the <ng-content> element with the select=[description] attribute, while content that uses the title attribute is projected into the <ng-content> element with the select=[title] attribute.

Step 4 – Open app.component.html and paste the following code.

<!-- As a heading -->
<nav class="navbar navbar-dark bg-primary">
<div class="container-fluid">
<span class="navbar-brand mb-0 h1">Multi Slot Content Projection</span>
</div>
</nav>
<div class="container">
<div class="row mt-5">
<div class="col-md-6">
<app-multi-slot-projection>
<span title>Manager's</span>
<span description>List of all working managers</span>
</app-multi-slot-projection>
</div>
<div class="col-md-6">
<app-multi-slot-projection>
<span title>Employee's</span>
<span description>List of all working employees</span>
</app-multi-slot-projection>
</div>
</div>
</div>

You can see that we called “app-multi-slot-projection” twice and passed different values for the attributes “description” and “title”.

Final Output

We passed different values for the descriptions “list of all working managers” and “list of all working employees,” respectively, for the title “Managers” and “Employees”.

list of working managers and employees

coma

Conclusion

Content projection in angular provides a powerful mechanism for reusability and flexibility in component-based development.

In this article, we explored two types of content projection: single-slot and multi-slot.

With single-slot content projection, we learned how to create a component that accepts and displays a single projected content.

Moving on to multi-slot content projection, we discovered a more advanced technique that allows us to project content into multiple slots within a component.

What is a content projection in Angular?

Content projection in Angular is a powerful feature that allows you to create more flexible and reusable components by allowing external content to be inserted into a component’s template.

Why use content projection in Angular?

Content projection in Angular enhances component reusability, allows dynamic template creation, separates concerns, promotes consistent UI patterns, and facilitates parent-child communication, leading to more flexible, maintainable, and adaptable components.

What are the benefits of content projection in Angular?

Content projection in Angular offers several benefits that enhance component reusability, flexibility, and maintainability: Component Reusability, Separation of Concerns, Dynamic Templates, Consistent UI Patterns, Ease of Testing etc.

Keep Reading

Keep Reading

  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?