Content Projection
Inputs pass data into a component. Sometimes you want to pass markup instead — a card that wraps arbitrary content, a dialog with a custom footer, a tab that holds any template. Angular calls this content projection, and the tool for it is the <ng-content> element.
Single-slot projection
Whatever the parent places between the component's opening and closing tags is inserted where <ng-content /> appears in the child's template:
import { Component, input } from '@angular/core';
@Component({
selector: 'app-card',
template: `
<section class="card">
<h3>{{ title() }}</h3>
<ng-content />
</section>
`,
styles: `.card { border: 1px solid #e0e0e0; border-radius: 8px; padding: 12px 16px; margin-bottom: 10px; }
h3 { margin: 0 0 6px; color: #dd0031; }`,
})
export class Card {
title = input('');
}
@Component({
selector: 'app-root',
imports: [Card],
template: `
<app-card title="Tip of the day">
<p>Use <code>track</code> with a unique id in every <code>@for</code>.</p>
</app-card>
<app-card title="Progress">
<progress value="7" max="10"></progress> 7 of 10 lessons
</app-card>
`,
})
export class App {}The card controls the frame and title; the parent controls the body. Note @ — inside a template, a literal @ character must be escaped because @ starts a block.
Multi-slot projection with select
Give <ng-content> a select attribute (any CSS selector) to create named slots. Content that matches no slot goes to the <ng-content> without select:
import { Component } from '@angular/core';
@Component({
selector: 'app-dialog',
template: `
<div class="dialog">
<header><ng-content select="[dialog-title]" /></header>
<div class="body"><ng-content /></div>
<footer><ng-content select="button" /></footer>
</div>
`,
styles: `
.dialog { border: 1px solid #ccc; border-radius: 8px; box-shadow: 0 4px 12px #0002; }
header { font-weight: bold; padding: 10px 14px; border-bottom: 1px solid #eee; }
.body { padding: 12px 14px; }
footer { padding: 8px 14px; text-align: right; background: #fafafa; }
`,
})
export class Dialog {}
@Component({
selector: 'app-root',
imports: [Dialog],
template: `
<app-dialog>
<button>Cancel</button>
<span dialog-title>Delete project?</span>
<p>This removes 14 tasks permanently.</p>
<button>Delete</button>
</app-dialog>
`,
})
export class App {}The order of the parent's markup doesn't matter — each element lands in the slot whose selector it matches, in document order within that slot.
How slot matching works
The rule — "first slot whose selector matches, otherwise the default slot" — is easy to model. This plain TypeScript version uses simplified selectors (tag or [attribute]):
interface ContentNode { tag: string; attrs: string[]; text: string; }
function matches(node: ContentNode, selector: string): boolean {
if (selector.startsWith('[')) return node.attrs.includes(selector.slice(1, -1));
return node.tag === selector;
}
function project(children: ContentNode[], selectors: string[]) {
const slots: Record<string, string[]> = { default: [] };
for (const s of selectors) slots[s] = [];
for (const child of children) {
const slot = selectors.find((s) => matches(child, s)) ?? 'default';
slots[slot].push(child.text);
}
return slots;
}
const children: ContentNode[] = [
{ tag: 'button', attrs: [], text: 'Cancel' },
{ tag: 'span', attrs: ['dialog-title'], text: 'Delete project?' },
{ tag: 'p', attrs: [], text: 'This removes 14 tasks.' },
{ tag: 'button', attrs: [], text: 'Delete' },
];
console.log(project(children, ['[dialog-title]', 'button']));{
default: [ 'This removes 14 tasks.' ],
'[dialog-title]': [ 'Delete project?' ],
button: [ 'Cancel', 'Delete' ]
}
Fallback content
<ng-content> may contain default markup that is shown when nothing is projected into that slot:
<footer>
<ng-content select="button">
<button>Close</button>
</ng-content>
</footer>Things to know
| Fact | Consequence |
|---|---|
| Projected content is created by the parent | Its bindings use the parent's fields, not the child's |
<ng-content> cannot be inside @if to toggle creation |
Content is still created; hide it with @if in the parent instead |
| Each piece of content is projected only once | Two <ng-content> with the same selector won't duplicate it |
ngProjectAs="[dialog-title]" |
Lets an element (e.g. an ng-container) match a slot it otherwise wouldn't |
Tip: For repeated or lazily created content (like list item templates), pass an
<ng-template>instead and render it withNgTemplateOutlet.
Test yourself
Where does content placed between <app-card> and </app-card> appear?
- Nowhere unless the child has an input for it
- Where the child's template has
<ng-content /> - At the end of the page
- In the parent's template after the card
What does <ng-content select="[footer]" /> receive?
- Elements with a
footerattribute - All <footer> elements only
- Every element
- Text nodes only
Whose properties can bindings inside projected content access?
- The child component's
- The parent component's (where the content is written)
- Both
- Neither