Sub topic 9
Reading projected content with content queries
This chapter assumes you can build a component with ng-content slots, you know signal-based input(), and you are comfortable with basic TypeScript generics. Projection puts the parent's markup in your template. A content query lets your component read the components and elements that were projected.
Why a wrapper needs to look inside
A tabs component has a problem. The parent writes <app-tab> elements between the <app-tabs> tags, and ng-content renders them. But the wrapper also needs to draw a button for each tab, and that needs each tab's title. ng-content cannot supply that data, because it only places nodes. A content query collects the projected instances so the wrapper can read them.
contentChildren
contentChildren returns a signal holding a read-only array of every matching projected item. The first argument is a component class, a directive class, or a template reference name.
import { Component, contentChildren, input } from '@angular/core';
@Component({
selector: 'app-tab',
template: `<ng-content />`,
})
export class TabComponent {
title = input.required<string>();
}
@Component({
selector: 'app-tabs',
template: `
<nav>
@for (tab of tabs(); track tab) {
<button>{{ tab.title() }}</button>
}
</nav>
<ng-content />
`,
})
export class TabsComponent {
tabs = contentChildren(TabComponent);
}The parent writes this:
<app-tabs>
<app-tab title="Profile">Profile form</app-tab>
<app-tab title="Billing">Billing form</app-tab>
</app-tabs>tabs() returns two TabComponent instances. Because title is a signal input, tab.title() updates the button when the parent changes the binding.
contentChild
contentChild returns a single match, or undefined if nothing matches. Add .required when the parent must supply the item. Angular then throws an error if the item is missing.
import { Component, TemplateRef, contentChild } from '@angular/core';
@Component({
selector: 'app-dialog',
template: `<ng-content />`,
})
export class DialogComponent {
footer = contentChild('footer', { read: TemplateRef });
firstInput = contentChild<HTMLInputElement>('field');
}A string argument matches a template reference variable such as #footer in the parent's template. The read option picks what the query returns from that node, for example TemplateRef or ElementRef.
What a content query can see
Content queries only find nodes that the parent wrote between your component's tags. They do not find nodes from your own template. Use viewChild and viewChildren for those.
| Query | Searches | Typical use |
|---|---|---|
| `viewChild`, `viewChildren` | The component's own template | Focus a button the component rendered |
| `contentChild`, `contentChildren` | Markup projected between the tags | Read tabs, columns, or items |
By default, contentChildren finds only direct children in the parent's markup. A match nested inside another element is skipped, unless that element is itself a projected node you are querying through. Pass { descendants: true } to search deeper.
tabs = contentChildren(TabComponent, { descendants: true });contentChild searches descendants by default, which differs from contentChildren.
Queries do not depend on slots
A query matches by what the parent wrote, not by where it was rendered. If a tab sits in a slot that the wrapper never renders, the query still finds it. This follows from the ownership rule you saw earlier: the parent creates the projected content whether or not it appears on screen.
When results are available
Content query signals are empty until Angular has created the projected content. Reading them in the constructor gives an empty result. Read them in a computed, in a template, or in an effect, and each will update when the results arrive.
import { Component, computed, contentChildren } from '@angular/core';
@Component({
selector: 'app-tab-summary',
template: `<p>{{ count() }} tabs</p><ng-content />`,
})
export class TabSummaryComponent {
tabs = contentChildren(TabComponent);
count = computed(() => this.tabs().length);
}Common errors
- Using
viewChildrenfor projected items. It returns an empty list because the items are not in your template. - Reading the signal in the constructor and getting an empty array.
- Expecting
contentChildrento find deeply nested matches withoutdescendants: true. - Forgetting that
contentChild.requiredthrows when the parent omits the item.
Exercise
A component app-list projects <app-item> elements, and its class needs the number of items. Which query do you use, and why does viewChildren(ItemComponent) return an empty array?
Show answer
Use contentChildren(ItemComponent) and read .length of the result. viewChildren searches only the component's own template, and the app-item elements were written in the parent's template, so they are not found.