Sub topic 11
Content Projection with ng-content
This chapter assumes you can build standalone components, use the imports array, and pass data with input(). Inputs work well for strings and numbers. Many components need to wrap whole blocks of markup instead, such as a card, a dialog, or a panel. Content projection handles this. The parent puts markup between the component's tags, and the child decides where that markup appears.
The default slot
An <ng-content /> element marks the spot in the child's template where projected content goes. Angular moves the parent's markup to that spot without changing it.
import { Component } from '@angular/core';
@Component({
selector: 'app-panel',
template: `
<div class="panel">
<ng-content />
</div>
`,
styles: `.panel { border: 1px solid #ccc; padding: 1rem; }`
})
export class PanelComponent {}A parent uses it like an ordinary HTML element:
import { Component } from '@angular/core';
import { PanelComponent } from './panel.component';
@Component({
selector: 'app-root',
imports: [PanelComponent],
template: `
<app-panel>
<p>This paragraph is projected into the panel.</p>
</app-panel>
`
})
export class AppComponent {}Named slots with select
The select attribute takes a CSS selector, so one component can have several slots. Content that matches the selector goes into that slot. Anything that matches no selector goes to the <ng-content /> that has no select.
import { Component } from '@angular/core';
@Component({
selector: 'app-card',
template: `
<header><ng-content select="[card-title]" /></header>
<section><ng-content /></section>
<footer><ng-content select="app-card-actions, .actions" /></footer>
`,
styles: `:host { display: block; border: 1px solid #ccc; padding: 1rem; }`
})
export class CardComponent {}
// Usage in another component's template:
// <app-card>
// <h2 card-title>Profile</h2>
// <p>Projected body content.</p>
// <div class="actions"><button>Save</button></div>
// </app-card>Attribute selectors such as [card-title] are the usual choice. They read clearly and don't depend on element type. Element selectors and class selectors also work, and you can list several selectors separated by commas.
Fallback content
From Angular 18 on, you can put markup inside <ng-content>. Angular shows that markup only when nothing is projected into the slot:
<header>
<ng-content select="[card-title]">Untitled</ng-content>
</header>Common errors
| Mistake | What happens | Fix |
|---|---|---|
| Wrapping `<ng-content>` in `@if` | The parent still creates the content, and its components still initialize | Use `ng-template` (covered later) for content that should load lazily |
| Two `<ng-content />` with no `select` | All unmatched content goes to the last one | Keep a single default slot |
| Styling projected markup from the child's `styles` | Encapsulated styles don't reach it | Style it in the parent, or style the slot's wrapper element |
| Wrapping slotted content in `<ng-container>` | The selector no longer matches | Add `ngProjectAs="[card-title]"` to the container |
Exercise
A modal component has <ng-content select="[modal-title]" /> and <ng-content />. A parent passes <h3 modal-title>Delete?</h3><p>This cannot be undone.</p><button>OK</button>. Where does each element end up?
Show answer
The h3 matches [modal-title], so it goes into the title slot. The p and the button match no selector, so both go into the default ng-content, in their original order.
Exercise
Why does putting <ng-content /> inside @if (open()) fail to prevent an expensive projected child from initializing?
Show answer
The parent creates projected content as part of its own template, whether or not the child renders the slot. The @if only controls whether the content is attached to the DOM. To defer creation, project an ng-template and render it on demand.