Sub topic 5
How change detection drives the hooks
This chapter assumes you know what each hook is called and have used @Input and template bindings. It explains why the hooks run when they do.
Hooks are side effects of a change detection pass
Angular does not call hooks on a timer. It calls them while it runs change detection, the process that compares your component state with the DOM and updates whatever differs. Each pass is called a tick.
A tick starts when something might have changed. With zone.js, the usual triggers are:
- a DOM event such as a click
- a timer
- a finished HTTP request
- a call to
ApplicationRef.tick()
The tick walks the component tree from the root down. For each component, Angular updates its bindings and calls the hooks that belong at that point in the walk.
Run-once hooks and run-every-tick hooks
The hooks fall into two groups:
| Hook | How often it runs |
|---|---|
| `ngOnInit`, `ngAfterContentInit`, `ngAfterViewInit` | Once, on the first pass that reaches that step |
| `ngOnChanges` | Only on ticks where a bound input changed |
| `ngDoCheck`, `ngAfterContentChecked`, `ngAfterViewChecked` | On every tick that reaches the component |
Because the second group runs so often, anything in it must be cheap. A slow ngDoCheck slows every click, keystroke and timer in the app.
Seeing it with OnPush
By default Angular checks every component on every tick. A component with ChangeDetectionStrategy.OnPush has its own view checked only when one of these happens:
- one of its inputs gets a new reference
- an event fires in its template
markForCheck()is called- a signal read in its template changes
import { AfterViewChecked, ChangeDetectionStrategy, Component, DoCheck } from '@angular/core';
@Component({
selector: 'app-counter',
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<button (click)="count = count + 1">Clicked {{ count }}</button>`,
})
export class CounterComponent implements DoCheck, AfterViewChecked {
count = 0;
ngDoCheck(): void {
console.log('ngDoCheck');
}
ngAfterViewChecked(): void {
console.log('ngAfterViewChecked');
}
}Place this component in a parent that has a running timer. Each timer tick logs ngDoCheck, because the parent is checked and calls the child's ngDoCheck. The child's view is skipped, so ngAfterViewChecked does not log. Clicking the button logs both. This is why ngDoCheck is the hook for custom checks on OnPush components: it still runs when the view check is skipped.
The second pass in development mode
In development mode, Angular runs a second pass after each tick. This pass calls no hooks. It only re-reads every binding and compares it with the value from the first pass. If a binding differs, Angular throws ExpressionChangedAfterItHasBeenCheckedError.
A typical cause is a child hook that changes state the parent has already rendered. For example, ngAfterViewInit might change a field that the parent template displays.
Exercise
A parent component is OnPush and its timer fires every second, but none of its inputs change. Which of ngOnChanges, ngDoCheck and ngAfterViewChecked run on that parent's child components on each timer tick, if the child is OnPush too?
Show answer
Only ngDoCheck runs on an OnPush child on those ticks, if its parent is checked. The child's inputs did not change, so ngOnChanges does not run. The child's view is skipped, so ngAfterViewChecked does not run either.