Sub topic 11
Content Projection with ng-content
Inputs let a parent pass data to a child. Sometimes the parent needs to pass markup instead: a heading, a form, or a button row that the child should place inside its own layout. Content projection is Angular's way to do this. The child component marks where outside content goes with <ng-content>, and the parent writes that content between the child's opening and closing tags.
This chapter assumes you can write a standalone component, use the imports array, and bind data with interpolation and property binding. You should also be comfortable with @if and @for.
The Single Default Slot
A component with one plain <ng-content /> takes all the content its parent places inside its tags and renders it at that spot.
import { Component } from '@angular/core';
@Component({
selector: 'app-panel',
template: `
<div class="panel">
<ng-content />
</div>
`,
styles: `.panel { border: 1px solid #ddd; border-radius: 6px; padding: 1rem; }`
})
export class PanelComponent {}
@Component({
selector: 'app-root',
imports: [PanelComponent],
template: `
<app-panel>
<p>Anything placed here appears inside the panel.</p>
<button (click)="clicks = clicks + 1">Clicked {{ clicks }} times</button>
</app-panel>
`
})
export class AppComponent {
clicks = 0;
}The projected <p> and <button> belong to AppComponent, not to PanelComponent. The interpolation {{ clicks }} and the (click) handler both resolve against AppComponent. The panel only decides where the content sits in the DOM. The parent keeps ownership of the content's data, events, and lifetime.
Named Slots with select
To place different pieces in different spots, give <ng-content> a select attribute. Its value is a CSS selector, and Angular matches it against the parent's content. Angular supports element, attribute, and class selectors, and combinations of them.
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=".card-actions" /></footer>
`,
styles: `
:host { display: block; border: 1px solid #ccc; padding: 1rem; }
footer { margin-top: 1rem; text-align: right; }
`
})
export class CardComponent {}
@Component({
selector: 'app-profile',
imports: [CardComponent],
template: `
<app-card>
<div class="card-actions">
<button>Edit</button>
</div>
<h2 card-title>Profile</h2>
<p>Ada Lovelace, analyst.</p>
</app-card>
`
})
export class ProfileComponent {}The order in the parent does not matter. The actions div comes first in the source, but it still renders in the footer, because the card's template sets the order. Anything that matches no select goes to the <ng-content> without a select, called the default slot.
Matching Rules
| Rule | Consequence |
|---|---|
| Only top-level nodes are matched | A `card-title` element nested inside a `div` does not reach the header slot. The whole `div` is matched instead. |
| Each node is projected into one slot | A node that matches two selectors goes to one slot only, never both. |
| Unmatched nodes need a default slot | If the card has no plain `<ng-content />`, unmatched content is silently dropped. |
| Selectors are static | `select` cannot be bound. `[select]="expr"` is not supported. |
Projecting Under Another Name with ngProjectAs
Sometimes you want to send a group of nodes into a named slot without adding a wrapper element. A wrapper would change your DOM and CSS. <ng-container> renders no element of its own, and the ngProjectAs attribute tells Angular which selector the container should match.
<app-card>
<ng-container ngProjectAs="[card-title]">
<h2>Settings</h2>
<small>Last saved today</small>
</ng-container>
<p>Body text.</p>
</app-card>Both the <h2> and the <small> land in the header, and no extra element appears in the DOM. You need ngProjectAs when one component passes its own projected content on to an inner component. Without it, the inner component's selectors would see an <ng-content> node instead of an attribute they recognize.
Fallback Content
Since Angular 18, you can put content inside <ng-content> itself. Angular shows that content when nothing is projected into the slot.
import { Component } from '@angular/core';
@Component({
selector: 'app-dialog',
template: `
<h3><ng-content select="[dialog-title]">Notice</ng-content></h3>
<ng-content>No details provided.</ng-content>
`
})
export class DialogComponent {}<app-dialog></app-dialog> renders "Notice" and "No details provided." If the parent passes <span dialog-title>Delete file?</span>, only the title fallback is replaced. Each slot decides on its own whether to use its fallback.
The Big Gotcha: Projection Is Not Conditional Rendering
The parent creates projected content, and the parent's view controls when that content lives. The child only chooses where the content appears. Two surprising behaviors follow from this:
- Hiding the slot does not prevent creation. If the child wraps
<ng-content />in@if (open()), the projected components are still created and their lifecycle hooks still run whileopen()is false. They are simply not attached to the DOM. A heavy chart or a component that fetches data on init does its work anyway. - A slot renders at most once. Putting
<ng-content />inside@fordoes not repeat the content. Angular projects each node into a single location.
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-collapsible',
template: `
<button (click)="open.set(!open())">{{ open() ? 'Hide' : 'Show' }}</button>
@if (open()) {
<ng-content />
}
`
})
export class CollapsibleComponent {
open = signal(false);
}This collapsible works visually. But any component projected into it is constructed when the parent renders, before anyone clicks "Show". Hiding it again does not destroy it. If you need the child to control when content is created, or to stamp it out many times, have the parent pass an <ng-template> instead. A later chapter covers <ng-template> and ngTemplateOutlet.
Styling Projected Content
Projected nodes are compiled with the parent's template, so they carry the parent's encapsulation attributes. The card's own styles reach its header, section, and footer wrappers, but they do not reach the <h2> the parent wrote. In practice, style the wrapper elements in the child, and leave the look of the projected content to the parent's styles. The styling chapter explains the mechanism behind this.
Choosing Between Inputs and Projection
| Need | Prefer |
|---|---|
| Plain text or a value the child formats | `input()` |
| Arbitrary markup, including other components | `<ng-content>` |
| Child controls when or how often the markup renders | `<ng-template>` passed in |
| Child must read or interact with the projected component | Projection plus `contentChild` (covered later) |
Exercise
A TabsComponent template contains only <ng-content select="app-tab" />. A parent writes <app-tabs><div><app-tab>One</app-tab></div><p>Help text</p></app-tabs>. What renders, and why?
Show answer
Nothing renders. Only top-level nodes are matched, so the div is checked against app-tab and fails, and the app-tab nested inside it is never considered. The p also matches nothing. Because the component has no default slot, both unmatched nodes are dropped. To fix it, remove the wrapper div or put ngProjectAs="app-tab" on the wrapper (as an ng-container), and add a plain <ng-content /> if the help text should appear.
Exercise
You wrap <ng-content /> in @if (expanded()) so that an expensive <app-report> projected by the parent loads only when the user expands the section. Logs show app-report running ngOnInit on page load. Explain why and name the fix.
Show answer
The parent's view creates projected content, so app-report is instantiated whether or not the child's @if attaches it to the DOM. The @if only controls placement. The fix is to have the parent pass an <ng-template> containing app-report, which the child renders with ngTemplateOutlet inside the @if. Alternatively, the parent can control the condition itself, or wrap the report in @defer.