Sub topic 4
The change detection cycle, top to bottom
This chapter assumes you know Angular components, template bindings, and how Zone.js signals that something may have changed (covered earlier). Here you follow one complete change detection cycle from start to finish.
What starts a cycle
In the default setup, Zone.js notifies ApplicationRef when an event handler, timer, or promise callback finishes. ApplicationRef then calls tick(). A tick is one pass of change detection over the whole application.
A tick does not ask "what changed?" It asks each view in turn, "are your binding values still the same as last time?" Angular cannot know which property changed, so it compares values.
The walk: root first, then downward
Every component has a view, and views form a tree that mirrors the component tree. A tick starts at the root view and refreshes it. Then it descends into each child view in template order.
For each view, Angular does these things in order:
- Evaluates the template's bindings and compares each result with the value stored from the previous check.
- Updates the DOM only for bindings whose value differs.
- Sets the inputs of child components and runs their
ngOnChanges,ngOnInit(first time only), andngDoCheckhooks. - Refreshes the child views, which repeats these steps one level down.
- Runs
ngAfterViewInit(first time only) andngAfterViewCheckedfor the views it just refreshed.
Parents are therefore checked before children, and a parent's view hooks run after all of its children are done. Data flows in one direction: from parent to child, never back up during the walk.
Seeing the order
This example logs the hooks so you can watch the order in the browser console. Click the button and read the output.
import { AfterViewChecked, Component, DoCheck } from '@angular/core';
@Component({
selector: 'app-child',
template: `<p>child</p>`,
})
export class Child implements DoCheck, AfterViewChecked {
ngDoCheck() { console.log('Child ngDoCheck'); }
ngAfterViewChecked() { console.log('Child ngAfterViewChecked'); }
}
@Component({
selector: 'app-parent',
imports: [Child],
template: `
<app-child />
<button (click)="clicks = clicks + 1">Clicked {{ clicks }}</button>
`,
})
export class Parent implements DoCheck, AfterViewChecked {
clicks = 0;
ngDoCheck() { console.log('Parent ngDoCheck'); }
ngAfterViewChecked() { console.log('Parent ngAfterViewChecked'); }
}After each click, the console shows Parent ngDoCheck, Child ngDoCheck, Child ngAfterViewChecked, Parent ngAfterViewChecked. The parent's ngDoCheck fires first because the parent is visited first. Its ngAfterViewChecked fires last because it waits for its child view.
Notice that Child has no inputs and never changes, yet its hooks ran on every click. Under the Default strategy, Angular checks every view in every tick.
The second pass in development
In development mode, Angular runs the tick twice. The second pass is checkNoChanges. It re-evaluates every binding and throws if any value differs from what the first pass stored. It never updates the DOM. It exists to catch state that changed during the walk, which is the source of ExpressionChangedAfterItHasBeenChecked. A later chapter covers that error in detail.
Practical consequences
- Template expressions run on every tick, so keep them cheap. A method call in a template runs once per check of that view.
- Do not change parent state from a child's
ngDoCheckorngAfterViewChecked. The parent has already been checked, so the change is caught by the second pass in development. - Lifecycle hooks such as
ngDoCheckrun often. Heavy work there slows every tick.
Exercise
A parent component renders two child components, A then B, in that order, with the Default strategy. In what order do ngAfterViewChecked hooks fire during one tick: parent, A, B?
Show answer
A, B, then the parent. Each child's view is refreshed and its view hooks fire before the parent's, because the parent's ngAfterViewChecked runs only after all of its child views have been checked.