Content projection with ng-content
Letting a parent supply markup a child component renders inside its own template — single- and multi-slot.
Content projection with ng-content
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `<ng-content>` renders whatever markup the parent placed *between* a component's opening and closing tags — content the child never sees as data, only as markup to project.
- `select` turns a single `<ng-content>` into several named slots, routing different parts of the parent's markup to different spots in the child's template.
- Content projection is a different relationship than `@Input()` — an input passes data the child reads and renders itself; projection passes markup the parent already decided how to render.
A different relationship than passing data
Everything in the last two lessons was about passing data — a value in through
@Input()/input(), an event out through @Output()/output(). Content
projection solves a different problem: letting a parent supply actual markup —
not a value, the HTML itself — and have a child component render that markup
inside its own template, in whatever spot the child’s template designates.
The clearest way to see why that’s different: a Card component shouldn’t need
to know in advance exactly what goes inside the card. It knows it wants to render
a bordered box with some padding — the specific heading, paragraph, or button
that ends up inside is the caller’s business, not the card’s.
<ng-content>: a placeholder for parent-supplied markup
@Component({
selector: 'app-card',
standalone: true,
template: `
<div class="card">
<ng-content></ng-content>
</div>
`,
})
export class CardComponent {}
<app-card>
<h2>Account settings</h2>
<p>Manage your profile and preferences.</p>
</app-card>
Whatever the parent places between <app-card> and </app-card> isn’t passed to
the component as data at all — CardComponent never sees “a heading and a
paragraph” as values it could inspect or transform. <ng-content> is purely a
placeholder marking where that already-rendered-by-the-parent markup gets
inserted. The card doesn’t know or care what’s inside it; it just knows where
“whatever’s inside” goes.
Multiple slots with select
A single <ng-content> only has one slot — everything the parent provides lands
in that one spot. Real components often need more structure than that: a card
with a distinct header region and a distinct body region, say. select turns
<ng-content> into a router, directing different pieces of the parent’s markup
to different slots based on a CSS selector:
template: `
<div class="card">
<header><ng-content select="[card-header]"></ng-content></header>
<div class="card-body"><ng-content select="[card-body]"></ng-content></div>
</div>
`
<app-card>
<h2 card-header>Account settings</h2>
<p card-body>Manage your profile and preferences.</p>
</app-card>
The parent marks its own markup with attributes (card-header, card-body) that
match the select selectors, and each piece is routed to its matching slot.
Anything the parent provides that doesn’t match any select falls through to a
plain, unselected <ng-content> if one exists — otherwise it’s simply not
rendered, which is worth testing for deliberately rather than discovering by
surprise.
Why this isn’t just @Input() with extra steps
It might look like you could achieve the same thing by passing an @Input()
containing a string of HTML, but that’s a much worse tool for this job — you’d
lose Angular’s own template compilation, binding, and change detection for
whatever you passed in, and you’d be reaching for [innerHTML] with all the
sanitization concerns that carries (covered later in this roadmap’s security
lesson). Content projection keeps the parent’s markup as real, fully-functional
Angular template content the whole way through — bindings, event handlers, and
all — it’s just rendered inside a different component’s template than the one
that wrote it.
This lesson’s video covers both single-slot and multi-slot projection with
worked examples — the linked “deeper” video on ngTemplateOutlet is worth a
second look here too, since passing an actual <ng-template> into a component
(rather than plain markup) is the more advanced cousin of the pattern you just
learned, letting the parent supply not just markup but a reusable template the
child can render more than once or with different context data.
Check yourself
A component's template is just `<div class="card"><ng-content></ng-content></div>`, used as `<app-card><h2>Title</h2><p>Body</p></app-card>`. What ends up inside the rendered `.card` div?
The `<h2>Title</h2>` and `<p>Body</p>` exactly as the parent wrote them — `<ng-content>` is a placeholder marking where that parent-supplied markup gets inserted, not a value the component reads or transforms.
Why would a `Card` component use `<ng-content select="[card-header]">` and `<ng-content select="[card-body]">` instead of one plain `<ng-content>`?
To create multiple named slots — so the parent can mark different pieces of its own markup (`<div card-header>`, `<div card-body>`) and have each one land in a specific spot inside the card's template, instead of all parent content getting dumped into one single location.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.