Sub topic 12
How hooks interleave between parent and child components
This chapter assumes you know the individual hooks (ngOnChanges, ngOnInit, ngDoCheck, the AfterContent and AfterView hooks, ngOnDestroy) and how to bind inputs. It shows how those hooks interleave when one component contains another.
The rule in one sentence
Angular runs the parent's hooks that come before its view is built, then enters each child fully, and only then runs the parent's AfterView hooks. A parent's ngAfterViewInit therefore runs after every child in its template has finished its own hooks.
An example with two levels
Suppose ParentComponent renders <app-child [value]="x" /> in its template, and ChildComponent has a hook for each stage.
import { Component, Input, OnChanges, OnInit, DoCheck, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-child',
standalone: true,
template: `<p>{{ value }}</p>`,
})
export class ChildComponent implements OnChanges, OnInit, DoCheck, AfterViewInit {
@Input() value = 0;
ngOnChanges() { console.log('child ngOnChanges'); }
ngOnInit() { console.log('child ngOnInit'); }
ngDoCheck() { console.log('child ngDoCheck'); }
ngAfterViewInit() { console.log('child ngAfterViewInit'); }
}
@Component({
selector: 'app-parent',
standalone: true,
imports: [ChildComponent],
template: `<app-child [value]="x" />`,
})
export class ParentComponent implements OnInit, DoCheck, AfterViewInit {
x = 1;
ngOnInit() { console.log('parent ngOnInit'); }
ngDoCheck() { console.log('parent ngDoCheck'); }
ngAfterViewInit() { console.log('parent ngAfterViewInit'); }
}On the first change detection pass the console shows:
- parent ngOnInit
- parent ngDoCheck
- child ngOnChanges
- child ngOnInit
- child ngDoCheck
- child ngAfterViewInit
- parent ngAfterViewInit
The parent's ngOnInit and ngDoCheck run first because Angular must evaluate the parent's template bindings before it can set the child's inputs. That is also why ngOnChanges on the child fires after the parent has started its pass.
Siblings and projected content
Siblings are processed in template order. Each sibling goes through its whole sequence before the next one starts for the pre-view hooks. The AfterContent hooks differ. For projected content, the host component's ngAfterContentInit runs after the projected children have had ngOnInit and ngDoCheck, but before the host's view is checked.
Destruction
Teardown also follows the tree. When a parent is removed, Angular destroys it together with its children. Do not rely on a specific order between a parent's and a child's ngOnDestroy. If a child needs a resource owned by the parent, make the parent's cleanup safe if the child is already gone.
Key points
- Inputs reach the child only after the parent's
ngDoCheckfor that pass. - A child's
ngOnInitruns once, before its firstngAfterViewInit. - The parent's
AfterViewhooks wait for all children in its view. - On later passes,
ngDoCheckruns for parent and child on every cycle, andngOnChangesruns only when an input changed.
Exercise
In the example above, why does the parent's ngAfterViewInit run after the child's ngAfterViewInit?
Show answer
The parent's view contains the child. Angular can only call the parent's AfterView hooks once every component in that view has been created and checked, so the child finishes its hooks first.