What does ngAfterViewChecked() do? When is it called?
In Angular, ngAfterViewChecked() is a lifecycle hook that is called after every check of the component's view and its child components.
Key points
- What it does:
- Lets you react to changes in the DOM and child components after they are rendered.
- Used to run logic that depends on the current state of the view after every change check.
- When it is called:
- After
ngAfterViewInit()- the first call happens right after the view is initialized. - Repeatedly, on every Angular change detection check, for example when data or events change.
Example:
ts
export class MyComponent implements AfterViewChecked {
ngAfterViewChecked() {
console.log('Component view and child components were checked');
}
}In other words, ngAfterViewChecked is a hook for tracking and reacting to changes in the component's view and its child elements on every Angular change detection cycle.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.