The menu card, the promo card and the order card on Jali's page share the same frame, but what's inside differs. An input can send text or a number to a child component. But an input is not a good way to send a whole piece of HTML, with its own heading, paragraph and button.
That's what content projection is for: HTML written by a parent component is shown inside a child component's template. The child component leaves a hole in its template. The parent component (the one that uses it) fills that hole with any markup (a piece of HTML) it likes. The hole is written <ng-content />. In other frameworks, a hole like this is called children (React) or <slot> (Vue). Picture Jali's pancake box. The box is always the same: logo, lid, string. What's inside changes, and the person who orders decides what goes in. A box with dividers even has dedicated spots: the name sticker on the lid, the pancake in the middle, the chilli sauce in a corner. The box is the child component, its contents are what the parent sends, the person who orders is the parent component, and each dedicated spot is a named hole.
ng-content: one hole
In this code, Card provides a frame with one hole, and App sends three elements into it:
import { Component } from '@angular/core'
@Component({
selector: 'app-card',
standalone: true,
template: `
<div class="card">
<ng-content />
</div>
`,
styles: `.card { border: 1px solid #ddd; border-radius: 12px; padding: 12px; }`,
})
export class Card {}
@Component({
selector: 'app-root',
standalone: true,
imports: [Card],
template: `
<app-card>
<h3 title>Special Egg Pancake</h3>
<p>Duck egg, beef.</p>
<button action (click)="order()">Order</button>
</app-card>
<p>Ordered: {{ ordered }}</p>
`,
})
export class App {
ordered = 0
order() {
this.ordered = this.ordered + 1
}
}
The DOM Angular builds:
<app-card>
<div class="card">
<h3 title="">Special Egg Pancake</h3>
<p>Duck egg, beef.</p>
<button action="">Order</button>
</div>
</app-card>
- Everything App writes between
<app-card>and</app-card>is moved to where<ng-content />is, in the order it was written. - That content still belongs to App.
(click)="order()"calls App'sorder()method, so clicking Order makes App showOrdered: 1. Card only provides the frame. - A Card with no
<ng-content />in its template doesn't show what App sends at all. - The
titleandactionattributes on App's elements have no effect here yet. They are used in the select section below.
The first exercise uses the same shape: a CardComponent with <div class="card"><ng-content /></div>. In the editor that card shows on its own, with no parent sending it content, so its .card is empty. What's graded is the shape of the template.
select: named holes
The select attribute on <ng-content> makes a named hole: a hole that only takes the elements that match what select holds. This is Card's template with three holes:
<!-- Card's template -->
<div class="card">
<header><ng-content select="[title]" /></header>
<div class="body"><ng-content /></div>
<footer><ng-content select="[action]" /></footer>
</div>
The same content from App lands here:
| Sent by App | Lands in |
|---|---|
<h3 title> |
<header>, because it matches [title] |
<p> |
<div class="body">, the hole with no select |
<button action> |
<footer>, because it matches [action] |
selectholds a CSS selector, the same kind of pattern CSS uses to pick elements. For example an attribute[title], a class.price, a tagh3, or several at once:[title], h3.- Elements that match no selector go to the hole without
select(called the plain hole here). - If there's no plain hole, elements that don't match are thrown away: they don't show, and there's no error. So if Card only has the
[title]and[action]holes, App's<p>appears nowhere. - Only the outermost elements App sends are matched.
<div><h3 title>...</h3></div>doesn't match[title], because the<div>is what's checked, and it has notitleattribute. So the whole<div>goes to the plain hole.
The second exercise uses this pattern: <header><ng-content select="[header]" /></header>, <main><ng-content /></main> and <footer><ng-content select="[footer]" /></footer>.
ngProjectAs: passing a hole along
To send several elements to one named hole at once, wrap them all in <ng-container action>. ng-container is a wrapper that doesn't become an element in the DOM. Its action attribute is only used for matching [action].
It's a different problem when a component passes its own hole along to another component. In this example, MenuCard's template uses Card. The title MenuCard receives from App must still go into Card's header:
<!-- MenuCard's template -->
<app-card>
<ng-content select="[title]" ngProjectAs="[title]" />
<p>Stock: 6</p>
</app-card>
Angular matches the outermost element MenuCard sends to Card, which is the <ng-content>, not the <h3 title>. Without ngProjectAs, that <ng-content> has no title attribute, so App's <h3 title> lands in Card's plain hole, next to Stock: 6. ngProjectAs="[title]" (treat this as [title]) makes that <ng-content> count as matching [title].
📌 ng-template and fallback content
ng-templatedoesn't show by itself.<ng-template>is the mould from the Built-in Directives lesson.<ng-template #promo><p>Today's promo</p></ng-template>produces nothing in the DOM until the mould is used. One way is<ng-container *ngTemplateOutlet="promo" />, which shows the#promomould in that spot (plusNgTemplateOutletinimports). The difference fromng-content:ng-contentshows its parent's content right away, whileng-templateonly stores a mould.- Fallback content. Since Angular 18, what's inside
<ng-content select="[action]"><button>Order</button></ng-content>shows when App sends nothing for that hole. - Sent content is still created when its hole is hidden. An
<ng-content />inside@if (false)shows nothing. But a component App sends is still created and its constructor still runs, because App creates it, not Card.
⚠️ Content that vanishes without a trace
- No hole without
select. Content that doesn't match is thrown away with no error. - An element wrapped in a
<div>.selectonly matches the outermost element, which is the<div>. - Two
<ng-content />withoutselect. Only the last one gets filled. Use one hole for each selector. - Forgetting
imports: [Card]. The console writesNG0304: 'app-card' is not a known element (used in the 'App' component template), and App's content shows bare inside the<app-card>tag, with no frame. - Exercise details. First exercise:
<ng-content />must be inside<div class="card">, not next to it. Second exercise: each<ng-content>goes straight inside<header>(withselect="[header]"),<main>(with noselect) and<footer>(withselect="[footer]").
TL;DR: Content projection shows HTML from a parent component inside a child component's template. <ng-content /> is a hole in the child's template. Everything the parent writes between the child component's opening and closing tags is moved into that hole. The content still belongs to the parent, (click) and {{ }} included. select="[title]" makes a named hole, which only takes outermost elements that have the title attribute. The rest goes to the hole without select. If there's no such hole, the rest is thrown away with no error. ngProjectAs="[title]" makes an element count as matching [title], mostly when a component passes its hole along to another component. ng-template is different: this mould doesn't show until it's used.
⚡ ng-content: a hole for sent content
Write:
@Component({
selector: 'app-card',
template: `<div class="card"><ng-content /></div>`,
})
export class Card {}
// used as: <app-card><h3>Special Egg Pancake</h3></app-card>
Meaning: <ng-content /> is a hole in Card's template. The parent component (the one that uses Card) writes the content between <app-card> and </app-card>, and Angular moves that content into the hole. The content still belongs to the parent, so a (click) in it calls the parent's method. Card only provides the frame.
In the first exercise, <ng-content /> must be inside <div class="card">, not next to it.
⚡ select: named holes
Write:
<header><ng-content select="[title]" /></header>
<div class="body"><ng-content /></div>
<footer><ng-content select="[action]" /></footer>
Guess first: select="[title]" makes a hole that only takes elements with a title attribute. If Card only has the [title] and [action] holes, and no plain hole (one without select), where does the parent's <p> go?
Nowhere. The <p> matches neither [title] nor [action], and there's no plain hole for it, so the <p> is thrown away: it doesn't show, and there's no error.
Meaning: A hole with select only takes the outermost sent elements that match its CSS selector. The rest goes to the plain hole, which is <div class="body"> in this code.
⚡ ng-content versus ng-template
Write:
<ng-template #promo><p>Today's promo</p></ng-template>
Meaning: ng-content shows what the parent component sends right away. ng-template only stores a mould. The <p>Today's promo</p> in this code doesn't show until the mould named #promo is used, for example through *ngTemplateOutlet="promo".