NG.I · Angular 22 practice question
A developer needs to interact with an element rendered inside their component's template, such as a third-party charting library instance, immediately after Angular has finished rendering that element. Which lifecycle hook is the most appropriate place to perform this initialization logic?
import { Component, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-chart',
standalone: true,
template: `<div><canvas #chartCanvas></canvas></div>`
})
export class ChartComponent implements AfterViewInit {
ngAfterViewInit() {
// Initialization logic goes here
}
}
Check answer and explanation
Correct answer: ngAfterViewInit
Explanation
The correct choice is "ngAfterViewInit" because this hook executes only after the component's view and all its child views have been fully initialized, allowing access to view queries.
Common trap
Using "ngOnInit" is incorrect because it runs before the component's view elements are available for inspection. "ngOnChanges" is for reacting to input property value changes, not for initial view setup.
Source: Angular docs: Component Lifecycle — ngAfterViewInit
Checked against the cited text on 2026-11-25.