Last updated Feb 11, 2023

Understanding Content Projection in Angular with ng-content

Master Angular content projection with ng-content.

Understanding Content Projection in Angular with ng-content

As Angular developers, we often encounter situations where we need to pass content from one component to another. This is where the concept of content projection comes in.

Content Projection in Angular with ng-content

Content projection allows us to pass content from a parent component to a child component and render it within the child component's template. In this article, we'll explore the ng-content directive, which is the backbone of content projection in Angular.

What is ng-content?

ng-content is a directive in Angular that acts as a placeholder for the content passed from the parent component.

The content can be any HTML element or text, and it will be rendered within the child component's template at the location of the ng-content directive.

The ng-content directive is essential for implementing content projection in Angular.

How to use ng-content?

Using ng-content is straightforward. Let's say we have a parent component that we want to pass content to a child component.

In the parent component's template, we'll wrap the content we want to pass in the child component's template with a tag that corresponds to the selector of the child component.

For example, let's say we have a child component with a selector of "app-child". To pass content to the child component, we would wrap the content in a tag with the same selector:

<app-child>
  <h1>This is some content</h1>
  <p>We want to pass this content to the child component</p>
</app-child>

In the child component's template, we'll add the ng-content directive to the location where we want the content to be rendered:

<ng-content></ng-content>

When the parent component is rendered, the content wrapped in the "app-child" tag will be inserted at the location of the ng-content directive in the child component's template.

Conclusion:

Content projection is a crucial concept in Angular and allows us to pass content between components.

The ng-content directive is the backbone of content projection and provides a placeholder for the content passed from the parent component. Understanding how to use ng-content is essential for Angular developers and will enable us to build more dynamic and interactive user interfaces.

Tags

Available for projectsLet’s build together

Need this built properly,
not just explained?

I take on Angular and NestJS work directly — auth systems, multi-tenant SaaS, API rewrites and the deploy pipeline underneath them. You talk to the person writing the code.

Projects delivered
20+ Projects delivered
Angular • NestJS
7+ years Angular • NestJS
Average reply time
1 day Average reply time

What I can help with

  • Auth and permissions systems
  • Multi-tenant SaaS foundations
  • API design and rewrites
  • Deploy pipelines and VPS setup
  • Bug fixing and performance optimization
Send me a message

Tell me about your project and I’ll get back with a realistic scope and price.

Turn
Your Ideas
Into Reality

Stay in the loop

Get the finished tutorials, projects and resources in your inbox.

Join 5,000+ developers and get new tutorials, source code, product updates and exclusive content — straight to your inbox.

  • No spam
  • Useful content only
  • Unsubscribe anytime

Subscribe to TutsCoder

Be the first to get new tutorials, projects and tips.

Join 5,000+ developers already learning with us.

Learn
Build
Grow

Discussion

Comments

Share your thoughts, ask questions or suggest improvements. Let’s learn and grow together!

Loading comments…

Join the discussion

Sign in to join the discussion. Your account also keeps your saved tutorials and path progress.