The Ultimate Guide to Content Projection in Angular for Beginner and Advanced Users

Posted by


Content Projection in Angular is a powerful feature that allows you to pass content into a component from the parent component’s template. This can be incredibly useful for creating reusable components that can be customized with different content based on the specific use case.

In this tutorial, we will discuss the basics of content projection in Angular, as well as some advanced techniques for handling more complex scenarios.

What is Content Projection?

Content projection in Angular is achieved using the <ng-content> tag. This tag acts as a placeholder inside the template of a component, where content from the parent component can be inserted.

Here’s a simple example to illustrate content projection:

// parent.component.html
<app-child>
  <h1>Hello, world!</h1>
</app-child>
// child.component.html
<div>
  <ng-content></ng-content>
</div>

In this example, the app-child component has a <ng-content> tag in its template. When the app-child component is used in the parent.component.html template with the <h1> element inside, the content of the <h1> element will be projected into the <ng-content> tag in the child.component.html template.

Basic Usage of Content Projection

Let’s dive into the basic usage of content projection in Angular.

  1. Create a new component using Angular CLI:
ng generate component my-component
  1. Update the template of the my-component component (my-component.component.html) to include an <ng-content> tag:
<div>
  <ng-content></ng-content>
</div>
  1. Use the my-component component in the parent component’s template (app.component.html):
<app-my-component>
  <h1>Hello, world!</h1>
</app-my-component>

In this example, the content within the <h1> element in app.component.html will be projected into the <ng-content> tag in my-component.component.html.

Handling Multiple Content Projections

You can use multiple <ng-content> tags in a component to handle different types of content projection. Here’s an example:

// my-component.component.html
<div>
  <ng-content select="header"></ng-content>
  <ng-content select="main"></ng-content>
</div>
// app.component.html
<app-my-component>
  <header><h1>Hello, world!</h1></header>
  <main><p>Lorem ipsum dolor sit amet.</p></main>
</app-my-component>

In this example, the content within the <header> and <main> elements in app.component.html will be projected into the <ng-content> tags with the corresponding select attribute in my-component.component.html.

Advanced Techniques

Content projection can be used in more complex scenarios to enable behavior like transclusion, slot-based content, and dynamic content projection.

  1. Transclude content with a default template:
// my-component.component.html
<div>
  <ng-content></ng-content>
  <ng-template #defaultContent>
    <p>Default content goes here.</p>
  </ng-template>
</div>
  1. Use the ng-template tag for slot-based content projection:
// my-component.component.html
<div>
  <ng-container *ngTemplateOutlet="contentTemplate || defaultContent"></ng-container>
  <ng-template #defaultContent>
    <p>Default content goes here.</p>
  </ng-template>
</div>
  1. Use @ContentChild or @ContentChildren decorators to access projected content programmatically in the component class.
// my-component.component.ts
import { ContentChild } from '@angular/core';

export class MyComponent {
  @ContentChild('header') header: ElementRef;
}

Conclusion

Content projection is a powerful feature in Angular that allows you to pass content into a component from the parent component’s template. By mastering content projection, you can create more flexible and reusable components that can be customized with different content for various use cases. I hope this tutorial has provided you with a solid understanding of content projection in Angular and how to leverage it effectively in your applications.

0 0 votes
Article Rating

Leave a Reply

41 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@DecodedFrontend
15 days ago

👉 To try everything Brilliant has to offer for free for a full 30 days, visit https://brilliant.org/DecodedFrontend
You’ll also get 20% off an annual premium subscription.

@BalonasulTudor
15 days ago

Also there are some some bugs with angular–ssr and ng-content that the page will not render unless, the ng-content is whitin a @defer selector

@mashab9129
15 days ago

thank you for keeping posting high quality content on Angular!

@aleksandrterentev6369
15 days ago

Gracias! Muy bien!

@yendrrek6703
15 days ago

Fabulous material. Thanks.

@azizulhoq3316
15 days ago

Best! Best!!

@Cooki3monstaJr
15 days ago

Would you be able to access re-projected content programmatically? For example, can you access the projected content from the AppComponent in the ButtonComponent via ContentChildren or ContentChild?

@avijitghosh9472
15 days ago

The content shared in this video about content projection is a course in itself 🙂

@ievgensvichkar2643
15 days ago

Thank you for the detailed ng-content video. Always learn smth new from your videos. In current one it was – the re-projection tricks and using a directive for getting a reference to a template. Thanks.

@gsgonzalez88
15 days ago

Excelent explanation! thanks!

@weradsaoud2018
15 days ago

Thank you, your videos on Angular are the best.

@franciscofdez8334
15 days ago

Great job. You resolved my doubts about this topic!. It would be really great if you combine portals with content projection. For instance: single main toolbar that render different action buttons depending on the "page" component you've been routed to (every page has its own buttons which will be projected to the main toolbar). PS: using cdkPortalOutlet or so.

@halynaua
15 days ago

Thanks, Dmytro. As usual, your explanation is amazing!

@dylanjhalltech8313
15 days ago

Thanks for the well thought out material. I think that a clear understanding of content projection is essential for making dynamic reusable components.

@tryhuma
15 days ago

You could consider making a course about angular cdk. thanks for video!

@marcolmos9079
15 days ago

The conditional component projection is just what I was looking for today!! I am working on my custom components library! Thanks!!!

@radvilardian740
15 days ago

the only one thing that prevent ppl to understand ur explanation is they dont know english 😂

@monfernape
15 days ago

Content projection in Angular 18 seems a bit more sophisticated than that of React. Slots are a great pattern, I wish we could have a dedicated slot keyword for this sorta stuff. Using classes as identifier would be a mistake too.

@ViktosB
15 days ago

Again amazing content! Thank you!

@TheMKilo8
15 days ago

Amazing content… ng-content 🙂

41
0
Would love your thoughts, please comment.x
()
x