Understanding the order of lifecycle events helps you control what happens when your Angular component starts, updates, or ends.
Lifecycle execution order mental model in Angular
ngOnChanges(changes: SimpleChanges) {}
ngOnInit() {}
ngDoCheck() {}
ngAfterContentInit() {}
ngAfterContentChecked() {}
ngAfterViewInit() {}
ngAfterViewChecked() {}
ngOnDestroy() {}Each method is called automatically by Angular at a specific time.
You only implement the methods you need in your component class.
ngOnInit() {
console.log('Component initialized');
}ngOnChanges(changes: SimpleChanges) {
console.log('Input changed', changes);
}ngOnDestroy() {
console.log('Component is about to be removed');
}This component shows how Angular calls lifecycle methods in order. When the input data changes, ngOnChanges runs first and updates the message. Then ngOnInit runs once after the first change. When the component is removed, ngOnDestroy runs to clean up.
import { Component, Input, OnChanges, OnInit, OnDestroy, SimpleChanges } from '@angular/core'; @Component({ selector: 'app-lifecycle-demo', template: `<p>{{message}}</p>` }) export class LifecycleDemoComponent implements OnChanges, OnInit, OnDestroy { @Input() data = ''; message = ''; ngOnChanges(changes: SimpleChanges) { this.message = `Input changed to: ${this.data}`; console.log('ngOnChanges called'); } ngOnInit() { console.log('ngOnInit called'); } ngOnDestroy() { console.log('ngOnDestroy called'); } }
ngOnChanges runs before ngOnInit and every time input changes.
ngOnInit runs once after the first ngOnChanges.
ngOnDestroy is your chance to clean up like stopping timers or unsubscribing.
Angular calls lifecycle methods in a specific order to manage your component.
ngOnChanges runs first on input changes, then ngOnInit runs once.
Use ngOnDestroy to clean up before the component is removed.
Practice
ngOnInit, ngOnChanges, ngAfterViewInit, or ngOnDestroy?Solution
Step 1: Understand lifecycle hooks on input change
When input properties change, Angular callsngOnChangesfirst to react to those changes.Step 2: Identify the first hook called on input update
ngOnInitruns only once after the firstngOnChanges, so it is not first.Final Answer:
ngOnChanges -> Option AQuick Check:
Input changes trigger ngOnChanges first [OK]
- Thinking ngOnInit runs before ngOnChanges
- Confusing ngAfterViewInit as first hook
- Assuming ngOnDestroy runs on input change
ngOnDestroy lifecycle hook in an Angular component?Solution
Step 1: Recall Angular lifecycle hook naming
Angular lifecycle hooks have specific method names starting with 'ng', songOnDestroyis correct.Step 2: Match method name exactly
Only ngOnDestroy() { console.log('Destroyed'); } uses the exact method namengOnDestroyrequired by Angular.Final Answer:
ngOnDestroy() { console.log('Destroyed'); } -> Option CQuick Check:
Lifecycle hooks start with 'ngOn' [OK]
- Using incorrect method names like onDestroy or destroy
- Missing 'ngOn' prefix
- Confusing lifecycle hook names with custom methods
export class MyComponent implements OnInit, OnChanges {
@Input() data: string;
ngOnChanges() { console.log('Changes detected'); }
ngOnInit() { console.log('Init called'); }
}What will be logged first when the component receives its first input value?
Solution
Step 1: Understand lifecycle order on first input
When the component first receives input, Angular callsngOnChangesbeforengOnInit.Step 2: Identify which log appears first
ThengOnChangesmethod logs 'Changes detected' first, thenngOnInitlogs 'Init called'.Final Answer:
Changes detected -> Option BQuick Check:
ngOnChanges runs before ngOnInit [OK]
- Assuming ngOnInit runs before ngOnChanges
- Thinking logs happen simultaneously
- Ignoring ngOnChanges on first input
ngOnDestroy method is not called when navigating away. What is the most likely cause?Solution
Step 1: Understand when ngOnDestroy runs
ngOnDestroyruns when Angular destroys the component instance.Step 2: Identify why it might not run on navigation
If the component is cached or reused (e.g., via route reuse strategy), it is not destroyed, songOnDestroywon't run.Final Answer:
The component is never destroyed because it is cached or reused -> Option AQuick Check:
ngOnDestroy runs only on actual destruction [OK]
- Confusing interface implementation with method call
- Using wrong method name
- Believing Angular never calls ngOnDestroy on navigation
ngOnChanges() { console.log('Changes'); }
ngOnInit() { console.log('Init'); }
ngAfterViewInit() { console.log('After View'); }
ngOnDestroy() { console.log('Destroy'); }If the component receives new input twice, then is removed from the page, what is the correct order of console logs?
Solution
Step 1: Order on first input and init
First input triggersngOnChanges, thenngOnInitruns once, thenngAfterViewInitafter view setup.Step 2: Order on second input and destroy
Second input triggersngOnChangesagain. Finally, when component is removed,ngOnDestroyruns.Final Answer:
"Changes", "Init", "After View", "Changes", "Destroy" -> Option DQuick Check:
ngOnChanges before ngOnInit; ngAfterViewInit after init; ngOnDestroy last [OK]
- Placing ngOnInit before first ngOnChanges
- Mixing order of ngAfterViewInit and ngOnChanges
- Forgetting ngOnDestroy runs last
Ready to prove it?
Pass the test and this pattern moves unknown
