Sub topic 7
Who owns projected content
This chapter assumes you can write standalone components, use ng-content with select, and read basic change detection terms such as "checked" and OnPush. It covers one rule: the component whose template contains the projected markup owns that markup. The sections below follow what that rule implies.
The ownership rule
Take this markup:
<app-card>
<p>{{ title }}</p>
</app-card>The <p> is written in the template of the component that contains <app-card>. Call that component the parent. CardComponent only decides where the <p> appears in the DOM.
Angular compiles the <p> and its binding as part of the parent's template. This has three consequences:
- Bindings evaluate against the parent's class, never the card's.
- The parent's change detection pass updates the bindings.
- Template references and
@letdeclarations belong to the parent template's scope.
The card contributes only a destination in the DOM.
Bindings resolve in the parent
Both components below define a property named title.
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-card',
template: `
<h3>{{ title }}</h3>
<ng-content />
`,
})
export class CardComponent {
title = 'Card title';
}
@Component({
selector: 'app-profile',
imports: [CardComponent],
template: `
<app-card>
<p>{{ title }} / {{ name() }}</p>
</app-card>
`,
})
export class ProfileComponent {
title = 'Profile title';
name = signal('Ada');
}The rendered output is:
- The
<h3>showsCard title, because the card's template reads the card's class. - The
<p>showsProfile title / Ada, because the paragraph is bound in the profile's template.
The card cannot read name. The profile cannot read the card's title from inside the projected markup. If the card needs to hand data to the parent's markup, plain projection can't do it. Use an ng-template with a context, covered in the chapter on lazy and repeated content.
Template references follow the same scope rule. If you write <input #search> inside projected content, search is visible in the parent's template. The card's template can't use it. The card has to find the element with a content query instead.
Change detection follows the owner
Because the parent owns the bindings, the parent's change detection updates them. A child that uses ChangeDetectionStrategy.OnPush can skip its own view, yet projected bindings still refresh.
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
@Component({
selector: 'app-frozen-card',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<section>
<p>Card-owned: {{ label }}</p>
<ng-content />
</section>
`,
})
export class FrozenCardComponent {
label = 'initial';
}
@Component({
selector: 'app-counter-page',
imports: [FrozenCardComponent],
template: `
<button (click)="count.set(count() + 1)">Add</button>
<app-frozen-card>
<p>Parent-owned count: {{ count() }}</p>
</app-frozen-card>
`,
})
export class CounterPageComponent {
count = signal(0);
}Clicking the button updates the projected paragraph, even though the card is OnPush and none of its inputs changed. If some code later assigned label a new value from outside Angular's tracking, the card's own paragraph would stay stale. The card would need an input change, an event from its own template, a signal read in its template, or markForCheck().
Two cautions apply:
- The parent still has to be checked. With an
OnPushparent, thecountsignal is what schedules the update. A plain mutable field would not. - If the projected content is a component, that component keeps its own
OnPushrules. The parent updates its inputs, and the component refreshes only when it is dirty.
Styles follow the owner too
With the default ViewEncapsulation.Emulated, Angular adds attributes such as _ngcontent-abc to every element in a component's template. Component styles are rewritten to match that attribute. The projected <p> carries the parent's attribute, so a card style for p does not reach it.
import { Component } from '@angular/core';
@Component({
selector: 'app-note',
styles: `
p { color: crimson; } /* matches only p elements in this template */
:host ::ng-deep p { color: crimson; } /* reaches projected p elements */
`,
template: `
<p>Note-owned paragraph</p>
<ng-content />
`,
})
export class NoteComponent {}The first rule colors only the note's own paragraph. The second uses ::ng-deep, which turns off encapsulation for that selector and is deprecated in Angular's documentation. It works, but it leaks. Prefer these options in roughly this order:
| Approach | How it works | Trade-off |
|---|---|---|
| Style in the parent | The parent's styles match its own template, including projected nodes | The parent must know the card's visual intent |
| Style hooks on the host | The card sets CSS custom properties, such as `--card-gap`, and the projected content reads them | Content must opt in with `var()` |
| `:host ::ng-deep` | The card styles any matching descendant | Leaks into nested components, and may be removed in future |
| `ViewEncapsulation.ShadowDom` with `::slotted(p)` | Uses native slots | Global styles stop applying inside the card |
Dependency injection follows the declaration tree
The element injector tree is built from the template where the elements are declared, not from the final DOM position of projected nodes. In the example below, <app-card-toggle> is written inside <app-card> in the parent template. That makes it a content child of the card, and it can inject the card.
import { Component, inject, signal } from '@angular/core';
@Component({
selector: 'app-card',
template: `
<header><ng-content select="app-card-toggle" /></header>
<div [hidden]="collapsed()"><ng-content /></div>
`,
})
export class CardComponent {
collapsed = signal(false);
toggle() {
this.collapsed.update((value) => !value);
}
}
@Component({
selector: 'app-card-toggle',
template: `<button (click)="card.toggle()">Toggle</button>`,
})
export class CardToggleComponent {
card = inject(CardComponent);
}
@Component({
selector: 'app-demo',
imports: [CardComponent, CardToggleComponent],
template: `
<app-card>
<app-card-toggle />
<p>Body that can be hidden</p>
</app-card>
`,
})
export class DemoComponent {}This is a common pattern for tabs, accordions and menus. A child registers with or talks to the component that wraps it, with no @Input plumbing.
The card's providers array is visible to its content children. Its viewProviders array is not. viewProviders exposes services only to the card's own template, so projected content can't see them.
The slot's condition does not change who creates the content
The card's [hidden] binding only hides the DOM. A more important case is a slot placed inside @if, which people expect to skip creating the content. The parent still creates the projected nodes and components, runs their constructors, and evaluates their bindings. The card decides only whether the nodes are attached to the DOM. The next chapter works through this behavior and its costs. For now, remember that ownership means the parent's template instantiates the content whether or not the card shows it.
What the card can and cannot do with projected content
The card can:
- Choose where the nodes appear, using
ng-contentandselect. - Read projected components and elements with
contentChildandcontentChildren. - Inject nothing from them directly, but receive injection requests from them, as with
CardToggleComponent.
The card cannot:
- Read or write the parent's class fields from its template.
- Wrap each projected node in extra markup.
ng-contentplaces the nodes as they are. - Re-render a node several times, because one projected node exists once. Use
ng-templatefor repeated content. - Change a projected node's bindings. Only the owner can.
Common mistakes
- Referencing the card's fields in projected content. Writing
{{ collapsed() }}inside<app-card>...</app-card>fails to compile, or reads an unrelated parent field with the same name. Expose state through the template-context approach or content queries. - Expecting card styles to reach projected nodes. Emulated encapsulation blocks this. Style in the parent or use CSS custom properties.
- Assuming
OnPushon the card freezes projected bindings. It doesn't. It freezes only the card's own view. - Putting side effects in projected components and then hiding them. The parent creates them regardless.
- Using
viewProvidersand expecting content children to see the service. Useproviders.
Exercises
Exercise
A parent template contains <app-card><p>{{ title }}</p></app-card>. Both the parent class and CardComponent define a title field. Which title does the paragraph display, and why?
Show answer
The paragraph displays the parent's title. The <p> and its binding are declared in the parent's template, so Angular compiles and evaluates the binding against the parent's class. The card only controls where the node is placed in the DOM.
Exercise
A card with ChangeDetectionStrategy.OnPush projects <p>{{ count() }}</p>, where count is a signal in the parent. The parent updates count from a button. Does the projected paragraph update? What would not update?
Show answer
The paragraph updates, because the parent owns the binding and the parent's view is refreshed when its signal changes. A binding in the card's own template that reads a plain mutable field would not update, since the OnPush card is not marked dirty.
Exercise
A projected app-card-toggle needs to call toggle() on the enclosing app-card. Which mechanism lets it get the card, and which provider option on the card would hide the card's service from it?
Show answer
The toggle can call inject(CardComponent). It is a content child of the card in the declaring template, so the card is on its element injector chain. A service listed in the card's viewProviders would be hidden from projected content, while one listed in providers would be visible.