Sub topic 7
Who owns projected content
This chapter assumes you know how to write a standalone component, bind to its fields in a template, and use a single <ng-content /> slot. It covers one idea: the component that declares projected markup owns it, and the component that hosts the slot only chooses where it appears.
The parent creates and binds the content
Consider a card that projects its content, and a page that uses the card.
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-card',
template: `<div class="card"><ng-content /></div>`,
})
export class CardComponent {
label = 'label from the card';
}
@Component({
selector: 'app-page',
imports: [CardComponent],
template: `
<app-card>
<p>{{ label }}</p>
<button (click)="increment()">Clicked {{ count() }} times</button>
</app-card>
`,
})
export class PageComponent {
label = 'label from the page';
count = signal(0);
increment() {
this.count.update(n => n + 1);
}
}The paragraph shows label from the page. The {{ label }} binding sits in PageComponent's template, so Angular evaluates it against PageComponent. CardComponent.label is never consulted. The same rule applies to event handlers: increment() runs on the page.
The card cannot read the projected content's data either. It has no access to the page's count, and the page's template cannot reach the card's label.
What the slot actually does
<ng-content /> is a placeholder for position. Angular compiles the projected nodes as part of the parent's view. When the card renders, the browser moves those existing nodes into the slot.
The block above is only a fence marker. The diagram follows.
Change detection follows the owner
Because the page owns the nodes, the page's change detection updates them. This holds even when the card opts out of checks:
import { ChangeDetectionStrategy, Component } from '@angular/core';
@Component({
selector: 'app-card',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<div class="card"><ng-content /></div>`,
})
export class CardComponent {}With this card, the button text in the previous example still updates after each click. The card's own template has no bindings, and the projected bindings are checked as part of the page's view. OnPush on the card limits only what belongs to the card.
The reverse also holds. If the card's template contained {{ label }} outside the slot, that binding would follow the card's own change detection rules.
Other things the owner decides
Ownership affects more than bindings.
- Dependency injection. Injection follows the element hierarchy as written in the parent's template. A component projected into
<app-card>can injectCardComponent, because in the template it sits inside the<app-card>element. It resolves providers from the page's declaration context, not from wherever the slot is placed. - Styles. Emulated view encapsulation tags each element with the attribute of the component that declared it. Projected elements carry the page's attribute. A selector like
.card pin the card's styles does not match a projected<p>. Style it in the page, or use:host ::ng-deepin the card as a last resort. - Lifecycle. The projected components are created, initialized and destroyed with the page's view. The card controls only where they appear.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Using a card field inside the projected markup | The binding reads the parent. It shows nothing or gives a compile error. | Expose the data through an input on the card, then bind it in the parent. |
| Styling projected elements from the card's stylesheet | The styles do not apply. | Style them in the parent, or use `::ng-deep` sparingly. |
| Expecting `OnPush` on the card to freeze projected content | The content still updates with the parent. | Put `OnPush` on the component that owns the content. |
Exercise
A card component has the field title = 'Card title'. A page template contains <app-card><h2>{{ title }}</h2></app-card>, and the page class has title = 'Page title'. The card is marked OnPush. Which title does the heading show, and which component's change detection updates it?
Show answer
It shows "Page title". The heading is created and bound by the page's template, so the binding reads PageComponent.title. The page's change detection updates it, and the card's OnPush setting does not affect it.