Sub topic 11
The Full Hook Order at a Glance
Earlier chapters covered each lifecycle hook on its own. This chapter puts them in one sequence. It shows what runs once, what runs on every change detection pass, and how the order changes when one component contains another. It assumes you know each hook's purpose, can write a standalone component, and know that Angular runs change detection (the process that checks bindings and updates the DOM) repeatedly while the app is alive.
The sequence for one component
For a single component, Angular calls the hooks in this order:
- The constructor runs when Angular creates the instance. Inputs are not set yet.
ngOnChangesruns if the component has bound inputs. It runs again whenever a bound input's reference changes.ngOnInitruns once, after the firstngOnChanges.ngDoCheckruns on every change detection pass.ngAfterContentInitruns once, after projected content (<ng-content>) is initialized.ngAfterContentCheckedruns after every check of the projected content.ngAfterViewInitruns once, after the component's own view and its child views are initialized.ngAfterViewCheckedruns after every check of the component's view.ngOnDestroyruns once, just before Angular removes the component.
Once versus every pass
The Init hooks mark the first time something becomes available. The Checked hooks and ngDoCheck repeat. The table summarizes this.
| Hook | How often | What is safe to use |
|---|---|---|
| constructor | Once | Dependency injection only. Inputs are not set. |
| `ngOnChanges` | On each input reference change | New input values, through `SimpleChanges` |
| `ngOnInit` | Once | Input values, for setup and first data loading |
| `ngDoCheck` | Every pass | Cheap custom comparisons |
| `ngAfterContentInit` | Once | Content queries such as `@ContentChild` |
| `ngAfterContentChecked` | Every pass | Content queries, with cheap work only |
| `ngAfterViewInit` | Once | View queries such as `@ViewChild` |
| `ngAfterViewChecked` | Every pass | View queries, with cheap work only |
| `ngOnDestroy` | Once | Cleanup of timers, listeners and subscriptions |
ngOnChanges has two conditions that are easy to forget. It never runs for a component with no bound inputs. It also compares references, so mutating an object that is already bound does not trigger it. Replace the object, or use ngDoCheck to detect the mutation.
Observing the order
This example logs every hook. The parent renders a child, so one run shows both the single-component order and the parent and child interleaving. The constructors are left out because they are not hooks. Both constructors run before any hook below, parent first.
import {
AfterContentChecked,
AfterContentInit,
AfterViewChecked,
AfterViewInit,
Component,
DoCheck,
Input,
OnChanges,
OnDestroy,
OnInit,
} from '@angular/core';
function log(who: string, hook: string): void {
console.log(`${who} ${hook}`);
}
@Component({
selector: 'app-probe',
standalone: true,
template: `<p>{{ label }}</p>`,
})
export class ProbeComponent
implements OnChanges, OnInit, DoCheck, AfterContentInit,
AfterContentChecked, AfterViewInit, AfterViewChecked, OnDestroy
{
@Input() label = '';
ngOnChanges(): void { log(this.label, 'ngOnChanges'); }
ngOnInit(): void { log(this.label, 'ngOnInit'); }
ngDoCheck(): void { log(this.label, 'ngDoCheck'); }
ngAfterContentInit(): void { log(this.label, 'ngAfterContentInit'); }
ngAfterContentChecked(): void { log(this.label, 'ngAfterContentChecked'); }
ngAfterViewInit(): void { log(this.label, 'ngAfterViewInit'); }
ngAfterViewChecked(): void { log(this.label, 'ngAfterViewChecked'); }
ngOnDestroy(): void { log(this.label, 'ngOnDestroy'); }
}
@Component({
selector: 'app-parent-probe',
standalone: true,
imports: [ProbeComponent],
template: `<app-probe [label]="childName" />`,
})
export class ParentProbeComponent
implements OnInit, DoCheck, AfterContentInit,
AfterContentChecked, AfterViewInit, AfterViewChecked
{
childName = 'child';
ngOnInit(): void { log('parent', 'ngOnInit'); }
ngDoCheck(): void { log('parent', 'ngDoCheck'); }
ngAfterContentInit(): void { log('parent', 'ngAfterContentInit'); }
ngAfterContentChecked(): void { log('parent', 'ngAfterContentChecked'); }
ngAfterViewInit(): void { log('parent', 'ngAfterViewInit'); }
ngAfterViewChecked(): void { log('parent', 'ngAfterViewChecked'); }
}If you bootstrap ParentProbeComponent, the first pass logs:
parent ngOnInit
parent ngDoCheck
parent ngAfterContentInit
parent ngAfterContentChecked
child ngOnChanges
child ngOnInit
child ngDoCheck
child ngAfterContentInit
child ngAfterContentChecked
child ngAfterViewInit
child ngAfterViewChecked
parent ngAfterViewInit
parent ngAfterViewCheckedThe parent has no bound inputs, so it logs no ngOnChanges. A second pass with no input changes logs only the repeating hooks:
parent ngDoCheck
parent ngAfterContentChecked
child ngDoCheck
child ngAfterContentChecked
child ngAfterViewChecked
parent ngAfterViewCheckedParent and child interleaving
The log shows the rule. A component's "before view" hooks run in two groups, and its view hooks run last:
- A component's
ngOnChanges,ngOnInitandngDoCheckrun before it renders its own template. - Its content hooks run next, still before its template's children are processed.
- Its children then go through their whole sequence, including their view hooks.
- Its own
ngAfterViewInitandngAfterViewCheckedrun last.
The parent's ngAfterViewInit therefore runs after every child's ngAfterViewInit. This is why a parent can safely query its children in that hook. It is also why a child cannot assume the parent's view is complete when the child's own view hooks run.
With several siblings, each sibling goes through its pre-render hooks and view hooks in template order.
Where render hooks fit
afterNextRender and afterRender are not steps in this per-component sequence. Angular runs them after it has finished rendering the application, and only in the browser. A callback registered in a constructor runs after all the hooks above have completed for that pass. Use them for DOM measurement or third-party widgets that must not run during server-side rendering.
Reading the order to avoid bugs
Most lifecycle bugs come from using a value before the hook that provides it.
- Input reads in the constructor give default values, because inputs are set later. Move that work to
ngOnInitorngOnChanges. - A
@ViewChildread inngOnInitis usuallyundefinedfor elements inside conditional blocks. The reliable place isngAfterViewInit. - Changing a bound value in
ngAfterViewInitorngAfterViewCheckedcan causeExpressionChangedAfterItHasBeenCheckedin development mode. Angular has already checked that binding in the current pass, and the dev-mode second check finds a different value. - Heavy work in
ngDoCheck,ngAfterContentCheckedorngAfterViewCheckedruns on every pass, so the whole app slows down.
Destruction
ngOnDestroy runs once, when Angular removes the component, for example when an @if block turns false or the router navigates away. After it runs, no further hooks fire. Release timers, event listeners and subscriptions there, or register the cleanup with DestroyRef. A component that is hidden with CSS but still in the DOM is not destroyed.
Exercise
A parent template contains <app-child [value]="x" />. On the first change detection pass, which runs first: the parent's ngAfterViewInit or the child's ngOnInit? Which runs first: the child's ngAfterViewInit or the parent's ngAfterViewInit?
Show answer
The child's ngOnInit runs first, because it belongs to the parent's rendering step, which happens before the parent's view hooks. The child's ngAfterViewInit runs before the parent's ngAfterViewInit, because a parent's view is complete only after all its children have finished.
Exercise
Your component has an input config bound to an object. The parent changes config.size = 10 without replacing the object. Does ngOnChanges run, and which hook can you use to notice the change?
Show answer
ngOnChanges does not run, because the reference did not change. Implement ngDoCheck and compare config.size with a stored previous value, or have the parent pass a new object instead of mutating the old one.