Sub topic 8
Working with the component's own view: the AfterView hooks
This chapter assumes you know how @ViewChild works, how change detection runs, and the difference between ngOnInit and the constructor. The AfterView hooks let you run code once Angular has created the component's template and its child components.
What the hooks are
Angular offers two hooks for the component's own view:
ngAfterViewInitruns once, after the component's view and its child views are initialized.ngAfterViewCheckedruns after every check of the view, including the first. It runs afterngAfterViewIniton the first pass.
A component's view is its template plus the child components and directives in it. Content passed in from a parent through <ng-content> is not part of the view. The AfterContent hooks cover that content.
Why view queries need this hook
A view query such as @ViewChild finds an element or component in the template. When the query is filled in depends on the query:
- A query with
{ static: true }is filled in beforengOnInit. It only works for elements that are always present, not for ones inside@if,@foror*ngIf. - A query without
static: trueis filled in beforengAfterViewInit. It is updated again whenever the matching elements change.
The constructor and ngOnInit run before most queries are available. So ngAfterViewInit is the usual place to read a view query.
import { AfterViewInit, Component, ElementRef, ViewChild } from '@angular/core';
@Component({
selector: 'app-focus-input',
standalone: true,
template: `<input #box type="text" />`,
})
export class FocusInputComponent implements AfterViewInit {
@ViewChild('box') box!: ElementRef<HTMLInputElement>;
ngAfterViewInit(): void {
this.box.nativeElement.focus();
}
}By the time ngAfterViewInit runs, box is set, so focus() is safe.
Common mistakes
Changing state that the template has already rendered causes ExpressionChangedAfterItHasBeenChecked in development mode. Suppose ngAfterViewInit sets this.height = this.box.nativeElement.offsetHeight and the template shows {{ height }}. Angular has already rendered height for this pass. In dev mode it checks again, sees a different value, and throws.
You have three ways to fix it:
- Use the value directly in the hook and don't bind it in the template.
- Store it in a signal. Updating a signal that the template reads schedules another change detection pass.
- Move the work into a render hook such as
afterNextRender, which a later chapter covers.
A plain setTimeout also hides the error, but it adds an extra pass and makes the flow harder to follow. Avoid it when a signal will do.
Other points to watch:
- Do not do heavy work in
ngAfterViewChecked. It runs after every check of the view, so slow code there slows the whole app. - Child components are initialized before the parent's
ngAfterViewInit. You can safely call methods on a@ViewChildchild component there. - Direct DOM access with
nativeElementdoes not work on the server during server-side rendering. Browser-only DOM work belongs in a render hook.
Exercise
A component reads this.chart.nativeElement in ngOnInit, and chart is an @ViewChild without static: true. The element is directly in the template. Why does this fail, and where should the code go?
Show answer
Without static: true, the query is filled in just before ngAfterViewInit, so chart is still undefined in ngOnInit. Move the code to ngAfterViewInit. Setting static: true would also work here, because the element is always present.