ViewChild, ContentChild, and signal queries
Reaching into a component's own template or projected content from its TypeScript code.
ViewChild, ContentChild, and signal queries
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- @ViewChild reaches into a component's own template; @ContentChild reaches into content that was projected into it — they query two different places, not the same place with different names.
- A template reference variable (`#ref`) only exposes a value inside the template itself; @ViewChild is what you need the moment that same reference has to be read from the component's TypeScript code.
- The signal-based viewChild()/contentChild() functions return a signal instead of a decorator-populated property, which removes the "is it set yet" timing question @ViewChild always carried.
A gap template reference variables can’t close
A template reference variable (#ref, from a few lessons back) gives you a
handle on an element or component instance, but only inside the template it’s
declared in — searchBox.focus() works fine from another binding in the same
template, and not at all from the component’s TypeScript code. Sometimes you
genuinely need that reach from the class itself: measuring an element’s size
after it renders, calling a method on a child component instance, reading the
actual rendered DOM node rather than a value passed through an input. That’s the
gap @ViewChild and @ContentChild exist to close.
@ViewChild: reaching into your own template
@Component({
template: `<input #searchBox />`,
})
export class SearchComponent implements AfterViewInit {
@ViewChild('searchBox') searchBox!: ElementRef<HTMLInputElement>;
ngAfterViewInit() {
this.searchBox.nativeElement.focus();
}
}
@ViewChild('searchBox') matches the template reference variable #searchBox
and makes it available as a class property — ElementRef wrapping the native DOM
element here, or a component instance if the reference points at a child
component instead of a plain element. The timing matters: this property isn’t
populated until the view has finished initializing, which is exactly why the read
happens in ngAfterViewInit, not ngOnInit — the lifecycle-hooks lesson covered
why those two moments are genuinely different, and this is the concrete case
where the difference bites if you get it wrong.
@ContentChild: reaching into projected content instead
@ViewChild only ever sees a component’s own template. It has no visibility
into content a parent projected in through <ng-content> — that’s a different
piece of DOM, owned by a different template, and @ContentChild is the query
built specifically for it:
@Component({ selector: 'app-tabs', template: `<ng-content></ng-content>` })
export class TabsComponent implements AfterContentInit {
@ContentChild(TabComponent) firstTab!: TabComponent;
ngAfterContentInit() {
this.firstTab.activate();
}
}
Same shape as @ViewChild, different target and a different paired lifecycle
hook (ngAfterContentInit instead of ngAfterViewInit) — because projected
content finishes initializing at a different point than the component’s own
view does. @ViewChildren/@ContentChildren are the plural versions, returning
a QueryList instead of a single match, for when more than one element or
component can match the query.
The signal-based versions remove a timing question
searchBox = viewChild<ElementRef<HTMLInputElement>>('searchBox');
focusSearch() {
this.searchBox()?.nativeElement.focus();
}
viewChild() and contentChild() do the same job as their decorator
counterparts, but return a signal instead of populating a plain property at some
point during the lifecycle. That has a real practical effect: instead of needing
to know which lifecycle hook is “safe” to read the query result in, you read the
signal — this.searchBox() — and it’s simply undefined until the match
exists, same as any other signal that hasn’t received a value yet. The query
result composes naturally with computed() and effect() too, the same way
input()’s signal does, which a plain @ViewChild property never let you do
without manually wrapping it yourself.
This lesson’s video makes the case directly for preferring the signal-based
queries going forward — the linked “deeper” video from Angular Training covers
viewChild/contentChild from a second angle, useful if the timing distinction
between the two isn’t fully clicking yet.
Check yourself
You have `<input #searchBox>` in a template, and `@ViewChild('searchBox') searchBox!: ElementRef;` in the class. In which lifecycle hook can you first safely read `this.searchBox.nativeElement`?
`ngAfterViewInit` — that's the hook Angular calls once the component's own view (everything in its template) has finished initializing. Reading a `@ViewChild` result any earlier, such as in `ngOnInit`, risks getting `undefined`, because the view it's querying hasn't finished being built yet.
Why would a tabs component use @ContentChild instead of @ViewChild to find its individual `TabComponent` children?
Because the individual tabs aren't part of the `TabsComponent`'s own template — they're markup the parent projected in between `<app-tabs>` and `</app-tabs>`, the same relationship content projection sets up. `@ViewChild` only sees the component's own template; `@ContentChild`/`@ContentChildren` is what queries projected content instead.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.