Explain it simply
ViewChild & ViewChildren, in plain language
@ViewChild is used in a parent component to access a child component or a DOM element directly.
Problem → Why → Solution
The problem — why this concept matters
A parent calling many child methods through ViewChild breaks component encapsulation.
A common mistake
One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.
Core concept — understand how it works
View queries retrieve a child component, directive, or template reference from a component's rendered view. Availability depends on view initialization and conditional rendering, so lifecycle and query semantics matter. Prefer inputs and outputs for ordinary communication; reserve ViewChild for focus, measurement, and imperative third-party APIs.
- input and state
- component and template
- rendered Angular UI
Explain the core concept and Angular data flow of ViewChild & ViewChildren
Core syntax or pattern
// child.component.ts
export class ChildComponent {
message = 'Hello from Child!';
showAlert() {
alert('Child method called!');
}
}
// parent.component.ts
import { Component, ViewChild, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
<app-child></app-child>
<button (click)="callChildMethod()">Call Child</button>
`
})
export class ParentComponent implements AfterViewInit {
@ViewChild(ChildComponent) child!: ChildComponent;
ngAfterViewInit() {
console.log(this.child.message);
}
callChildMethod() {
this.child.showAlert();
}
}View queries retrieve a child component, directive, or template reference from a component's rendered view. Availability depends on view initialization and conditional rendering, so lifecycle and query semantics matter. Prefer inputs and outputs for ordinary communication; reserve ViewChild for focus, measurement, and imperative third-party APIs.
Read the code step by step
- input and state
// child.component.ts@ViewChild is used in a parent component to access a child component or a DOM element directly.
- component and template
<app-child></app-child>View queries retrieve a child component, directive, or template reference from a component's rendered view. Availability depends on view initialization and conditional rendering, so lifecycle and query semantics matter. Prefer inputs and outputs for ordinary communication; reserve ViewChild for focus, measurement, and imperative third-party APIs.
- rendered Angular UI
}Use ViewChild to focus an input when a dialog opens and test the query before and after conditional rendering.
- input and state
- component and template
- rendered Angular UI
Success
Explain the core concept and Angular data flow of ViewChild & ViewChildren
Failure
A parent calling many child methods through ViewChild breaks component encapsulation.
@ViewChild is used in a parent component to access a child component or a DOM element directly.
// child.component.ts
export class ChildComponent {
message = 'Hello from Child!';
showAlert() {
alert('Child method called!');
}
}
// parent.component.ts
import { Component, ViewChild, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
<app-child></app-child>
<button (click)="callChildMethod()">Call Child</button>
`
})
export class ParentComponent implements AfterViewInit {
@ViewChild(ChildComponent) child!: ChildComponent;
ngAfterViewInit() {
console.log(this.child.message);
}
callChildMethod() {
this.child.showAlert();
}
}(The parent can directly access the child component's properties and methods)Real-world usage
How to apply it in a real project
Use ViewChild to focus an input when a dialog opens and test the query before and after conditional rendering.
Mini challenge
Build it yourself
Rebuild the ViewChild & ViewChildren example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
// child.component.ts
export class ChildComponent {
message = 'Hello from Child!';
showAlert() {
alert('Child method called!');
}
}
// parent.component.ts
import { Component, ViewChild, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
<app-child></app-child>
<button (click)="callChildMethod()">Call Child</button>
`
})
export class ParentComponent implements AfterViewInit {
@ViewChild(ChildComponent) child!: ChildComponent;
ngAfterViewInit() {
console.log(this.child.message);
}
callChildMethod() {
this.child.showAlert();
}
}Expected: Explain the core concept and Angular data flow of ViewChild & ViewChildren
Show hint
Use ViewChild to focus an input when a dialog opens and test the query before and after conditional rendering.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- input and state → component and template → rendered Angular UI
- View queries retrieve a child component, directive, or template reference from a component's rendered view. Availability depends on view initialization and conditional rendering, so lifecycle and query semantics matter. Prefer inputs and outputs for ordinary communication; reserve ViewChild for focus, measurement, and imperative third-party APIs.
- A parent calling many child methods through ViewChild breaks component encapsulation.
- Use ViewChild to focus an input when a dialog opens and test the query before and after conditional rendering.
Angular — Components — Angular