Content Projection with ng-content in Angular
Build truly reusable and flexible UIs by projecting content from a parent component directly into a child's template.
What is Content Projection?
Content Projection is a powerful Angular feature that lets you create flexible, reusable components. The idea is to make a component with a placeholder, and then allow another component (the parent) to "project" its own HTML content into that placeholder. The core of this mechanism is the <ng-content>
element.
Projecting Content from a Parent
Imagine you have a generic <app-card>
component. Instead of hard-coding its content, you can use it like a standard HTML tag and place your desired content directly inside it. This content from the parent is what will be projected.
Multi-Slot Projection with `select`
What if your component needs multiple placeholders, like a header, body, and footer? The select
attribute on <ng-content>
allows you to create specific "slots". You can select content based on a CSS selector like a tag name (select="h1"
), a class (select=".card-footer"
), or an attribute (select="[card-header]"
).
The Result: A Flexible Component Library
By mastering <ng-content>
, you move from building static, single-purpose components to creating a flexible UI library. This pattern is fundamental for creating shareable wrappers, layouts, and data display components that adapt to any context.
Practice Zone
Interactive Test 1: Match the Concept
Match the <ng-content>
syntax to its correct purpose.
Arrastra en el orden correspondiente.
Arrastra las opciones:
Completa el código:
Interactive Test 2: Complete the Code
Rellena los huecos en cada casilla.
// child-card.component.ts @Component({ selector: 'app-child-card', template: ` <div class="card"> <div class="header"> </div> <div class="body"> </div> </div> ` }) // parent.component.ts @Component({ template: ` <app-child-card> <h1 class="header">My Title</h1> <p>This is the main content.</p> </app-child-card> ` })
Practice Example: Code Editor
Create a parent component that projects a header with a `header-content` class and a footer with a `footer-content` class into a child component that uses `ng-content` with the `select` attribute to place them correctly.
Content Projection in Action
Content projection is a cornerstone of building a scalable and maintainable component architecture in Angular. Here are some common, powerful use cases.
1. The Generic Card Component
This is the classic example. You create a card with styling and structure, but leave the content areas open with <ng-content>
. This allows any other component to use your card component to display anything from user profiles to product information, ensuring a consistent look and feel.
// usage in parent.component.html
<app-card>
<h2 class="card-title">User Profile</h2>
<p>Details about the user...</p>
</app-card>
User Profile
Details about the user...
2. Building a Modal/Dialog Wrapper
Modals often have a consistent frame (overlay, close button, header/footer areas) but highly dynamic body content. Content projection is perfect for this. Your `ModalComponent` handles the logic of showing/hiding, while the parent component simply projects the form, confirmation message, or data that needs to be displayed inside.
// modal-wrapper.component.html
<div class="modal-header">
<ng-content select="[modal-title]"></ng-content>
</div>
<div class="modal-body">
<ng-content></ng-content>
</div>
3. Creating Layout Components
For more complex page structures, you can build layout components like `two-column-layout` or `sidebar-layout`. These components define the CSS grid or flexbox structure and provide named slots using `select` for the parent to project content into, such as a sidebar, main content area, or header.
<app-sidebar-layout>
<div nav-area>...Navigation Links</div>
<div main-content>...Page Content</div>
</app-sidebar-layout>
Practical Takeaway: Stop copying and pasting HTML structures. If you find yourself building similar-looking components with different inner content, it's a perfect opportunity to refactor and use a single, flexible component with `<ng-content>`.
Content Projection Glossary
- Content Projection
- The mechanism in Angular for passing HTML content from a parent component's template into a specified location in a child component's template.
- <ng-content>
- The core element used in a child component's template to mark the spot where projected content should be inserted. It acts as a placeholder.
- select Attribute
- An attribute used on the `<ng-content>` element to specify which content should be projected into that particular slot. It uses CSS selectors (e.g., `select=".my-class"`, `select="[my-attribute]"`).
- Single-slot Projection
- The most basic form of content projection, using a single `<ng-content>` without a `select` attribute. It projects all content from the parent into one place.
- Multi-slot Projection
- A more advanced pattern where a component has multiple `<ng-content>` elements, each with a different `select` attribute. This allows the parent to project different pieces of content into different, specific slots.