Sub topic 11
The full hook order at a glance
This chapter assumes you know how to build a standalone Angular component, bind inputs in a template, and read ngOnInit and ngOnDestroy from earlier chapters. Here you put every hook into one sequence.
The sequence
Angular calls hooks in a fixed order. Some run once, and some run on every change detection pass.
| Hook | Runs | Typical use |
|---|---|---|
| `constructor` | Once | Dependency injection, field setup |
| `ngOnChanges` | Before `ngOnInit`, then whenever a bound input changes | React to input changes |
| `ngOnInit` | Once | Initial setup that needs inputs |
| `ngDoCheck` | Every pass | Custom change checks |
| `ngAfterContentInit` | Once | Read projected content |
| `ngAfterContentChecked` | Every pass | Rarely needed |
| `ngAfterViewInit` | Once | Read view queries, touch child views |
| `ngAfterViewChecked` | Every pass | Rarely needed |
| `ngOnDestroy` | Once | Cleanup |
ngOnChanges is skipped if the component has no bound inputs, or if none of them changed during that pass.
On the first pass the order is: ngOnChanges, ngOnInit, ngDoCheck, ngAfterContentInit, ngAfterContentChecked, ngAfterViewInit, ngAfterViewChecked. On later passes only ngOnChanges (when an input changed), ngDoCheck, ngAfterContentChecked and ngAfterViewChecked run.
Seeing it in a log
This component logs each hook. Bind value in a parent and change it to watch the later passes.
import {
AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit,
Component, DoCheck, Input, OnChanges, OnDestroy, OnInit,
} from '@angular/core';
@Component({
selector: 'app-trace',
standalone: true,
template: `<p>{{ value }}</p><ng-content />`,
})
export class TraceComponent
implements OnChanges, OnInit, DoCheck, AfterContentInit,
AfterContentChecked, AfterViewInit, AfterViewChecked, OnDestroy {
@Input() value = '';
constructor() { console.log('constructor'); }
ngOnChanges() { console.log('ngOnChanges'); }
ngOnInit() { console.log('ngOnInit'); }
ngDoCheck() { console.log('ngDoCheck'); }
ngAfterContentInit() { console.log('ngAfterContentInit'); }
ngAfterContentChecked() { console.log('ngAfterContentChecked'); }
ngAfterViewInit() { console.log('ngAfterViewInit'); }
ngAfterViewChecked() { console.log('ngAfterViewChecked'); }
ngOnDestroy() { console.log('ngOnDestroy'); }
}Where render hooks fit
afterNextRender and afterRender are not methods on the class. You register them in an injection context, usually the constructor. They run after Angular has finished rendering, and only in the browser. Use them for DOM work that must not run during server-side rendering.
Parent and child order
A parent and its children share one pass. The parent's ngOnInit and ngDoCheck run before its children are created. The parent's ngAfterViewInit runs after all its children's view hooks have finished. The next chapter covers this interleaving in detail.
Common mistakes
- Reading an input in the constructor. Inputs are set later, so the value is still the default.
- Reading a
@ViewChildbeforengAfterViewInit. It may not be resolved yet. - Changing bound state inside an
AfterViewCheckedhook. This can raiseExpressionChangedAfterItHasBeenChecked.
Exercise
A component has a bound input that changes once after the first render. List which hooks run on the second change detection pass, in order.
Show answer
ngOnChanges, ngDoCheck, ngAfterContentChecked, ngAfterViewChecked. The Init hooks and the constructor do not run again.
Exercise
Why does the parent's ngAfterViewInit run after its child's ngAfterViewInit?
Show answer
The parent's view includes its children's views, so Angular finishes checking and initializing the children first. Only then is the parent's view complete.