What is the difference between ViewChild and ContentChild in Angular?
Compare Angular ViewChild and ContentChild: where each queries elements, their lifecycle hooks, plural QueryList forms, and when to use each, with examples.
Expected Interview Answer
@ViewChild queries an element, directive, or component that lives in the component's own template (its view), while @ContentChild queries content that was projected into the component from a parent through ng-content.
The distinction follows where the referenced item is declared. ViewChild results are available after ngAfterViewInit, whereas ContentChild results are available after ngAfterContentInit. Their plural forms, @ViewChildren and @ContentChildren, return QueryLists of matches. Choosing the wrong one means the query returns undefined because the element is not where that decorator looks.
- ViewChild accesses elements the component itself defines
- ContentChild accesses content projected from a parent
- Correct lifecycle hook ensures the reference is ready
- Plural forms provide live QueryLists of matches
- Enables direct interaction with child DOM, directives, or components
AI Mentor Explanation
ViewChild is checking the players in your own team's dressing room — people you selected. ContentChild is checking the guest players a visiting captain sent onto your field through the pavilion gate. Both are on the pitch, but one you named yourself and the other arrived from outside.
Step-by-Step Explanation
Step 1
Identify where the element lives
Decide if the target is in your own template (view) or projected from a parent via ng-content (content).
Step 2
Pick the decorator
Use @ViewChild for view elements and @ContentChild for projected content; add { static: false } or true as needed.
Step 3
Declare the query
Add a property like @ViewChild('ref') child, referencing a template variable, component, or directive.
Step 4
Read in the right hook
Access ViewChild in ngAfterViewInit and ContentChild in ngAfterContentInit, when each is resolved.
Step 5
Use plural forms if needed
Use @ViewChildren or @ContentChildren to get a QueryList of all matches instead of a single one.
What Interviewer Expects
- Clear view-versus-projected-content distinction
- Correct lifecycle hooks for each query
- Knowledge of the plural QueryList forms
- Understanding of the static option
- A concrete example of when each is used
Common Mistakes
- Using ViewChild to query projected content
- Reading a ViewChild in ngOnInit instead of ngAfterViewInit
- Confusing ContentChild with Input properties
- Forgetting that projected content resolves in ngAfterContentInit
Best Answer (HR Friendly)
“ViewChild grabs an element that the component defines in its own template, while ContentChild grabs content that a parent component passed in from the outside. The difference is simply where that element originally comes from.”
Code Example
import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-panel',
template: '<input #box>'
})
export class PanelComponent implements AfterViewInit {
@ViewChild('box') box!: ElementRef<HTMLInputElement>;
ngAfterViewInit() {
this.box.nativeElement.focus();
}
}import { Component, ContentChild, AfterContentInit } from '@angular/core';
@Component({
selector: 'app-tab',
template: '<ng-content></ng-content>'
})
export class TabComponent implements AfterContentInit {
@ContentChild('title') title: any;
ngAfterContentInit() {
console.log(this.title);
}
}Follow-up Questions
- In which lifecycle hooks are ViewChild and ContentChild available?
- What do ViewChildren and ContentChildren return?
- What does the { static: true } option change?
- Can ViewChild access a projected element? Why or why not?
MCQ Practice
1. What does @ContentChild query?
ContentChild queries content projected via ng-content from a parent component.
2. When is a @ViewChild reference reliably available?
View children are resolved by the ngAfterViewInit lifecycle hook.
3. What does @ContentChildren return?
ContentChildren returns a QueryList of all matching projected items.
Flash Cards
What does @ViewChild query? — An element, directive, or component in the component's own template (view).
What does @ContentChild query? — Content projected into the component from a parent via ng-content.
Lifecycle hook for ViewChild vs ContentChild? — ngAfterViewInit for ViewChild; ngAfterContentInit for ContentChild.
What do the plural forms return? — ViewChildren and ContentChildren return a QueryList of matches.