Sub topic 7
Working with projected content: the AfterContent hooks
This chapter assumes you know ng-content, the ngOnInit hook, and basic template bindings.
What projected content is
Projected content is markup a parent puts between a component's tags. The component renders it where it places <ng-content />. The parent creates and owns that markup. The receiving component only decides where it appears.
The component cannot see projected content through ViewChild. It uses content queries instead: @ContentChild for one match and @ContentChildren for many.
The two hooks
ngAfterContentInitruns once. It runs after Angular has projected the content into the component and resolved its content queries.ngAfterContentCheckedruns after every check of the projected content. That includes the first check, right afterngAfterContentInit.
Example: a tabs component
TabsComponent reads the titles of the TabComponent instances the parent projects into it.
import { AfterContentInit, Component, ContentChildren, Input, QueryList } from '@angular/core';
@Component({
selector: 'app-tab',
standalone: true,
template: `<ng-content />`,
})
export class TabComponent {
@Input() title = '';
}
@Component({
selector: 'app-tabs',
standalone: true,
template: `
<p>Tabs: {{ titles.join(', ') }}</p>
<ng-content />
`,
})
export class TabsComponent implements AfterContentInit {
@ContentChildren(TabComponent) tabs!: QueryList<TabComponent>;
titles: string[] = [];
ngAfterContentInit(): void {
this.titles = this.tabs.map((tab) => tab.title);
this.tabs.changes.subscribe(() => {
this.titles = this.tabs.map((tab) => tab.title);
});
}
}
@Component({
selector: 'app-root',
standalone: true,
imports: [TabsComponent, TabComponent],
template: `
<app-tabs>
<app-tab title="Overview">First</app-tab>
<app-tab title="Details">Second</app-tab>
</app-tabs>
`,
})
export class AppComponent {}The tabs query is empty in the constructor and in ngOnInit, unless the query is declared with { static: true }. A static query works only for content that is not inside @if, @for or other structural blocks. By ngAfterContentInit, the query is filled. Each TabComponent already has its title input set.
QueryList.changes emits when the parent adds or removes matching content later. Angular completes that observable when the component is destroyed, so this subscription needs no manual cleanup.
Why assigning `titles` is safe here
ngAfterContentInit runs before Angular checks the component's own template. The assignment to titles therefore happens before {{ titles.join(', ') }} is evaluated. The same assignment in ngAfterViewInit would change a value after it was already rendered. In development mode that throws ExpressionChangedAfterItHasBeenChecked.
Common errors
- Reading a content query in
ngOnInitor the constructor and gettingundefinedor an empty list. - Using
@ViewChildfor projected elements. It finds nothing, because the elements belong to the parent's template. - Doing heavy work in
ngAfterContentChecked. It runs on every change detection pass, so keep it cheap and free of side effects. - Expecting
ngAfterContentInitto fire again when projected items change. It fires once. UseQueryList.changesfor later updates.
Exercise
A component declares @ContentChild(HeaderDirective) header!: HeaderDirective; and reads this.header.label in ngOnInit. The parent projects a HeaderDirective inside an @if block. Why does this fail, and which hook fixes it?
Show answer
Content inside @if is not static, so the query is not resolved when ngOnInit runs and header is undefined. Read it in ngAfterContentInit, where content queries are resolved. If the @if condition can change later, also handle undefined, because the content may not exist at that point.