0
0
Angularframework~10 mins

Lifecycle execution order mental model in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Lifecycle execution order mental model
Component Creation
Constructor runs
ngOnChanges if inputs change
ngOnInit runs
ngDoCheck runs
Render View
User Interaction or Data Change
ngOnChanges if inputs change
ngDoCheck runs
Render View
Component Destroy
ngOnDestroy runs
This flow shows the order Angular calls lifecycle hooks from component creation to destruction, including changes and checks.
Execution Sample
Angular
constructor() { console.log('constructor'); }
ngOnChanges() { console.log('ngOnChanges'); }
ngOnInit() { console.log('ngOnInit'); }
ngDoCheck() { console.log('ngDoCheck'); }
ngOnDestroy() { console.log('ngOnDestroy'); }
Logs lifecycle hook calls in the order Angular executes them during component lifecycle.
Execution Table
StepTriggerHook CalledDescriptionOutput
1Component createdconstructorComponent class instance createdLogs 'constructor'
2Input properties setngOnChangesDetect input property changesLogs 'ngOnChanges'
3After first inputs setngOnInitInitialize component after inputsLogs 'ngOnInit'
4Change detection cyclengDoCheckCustom change detection logicLogs 'ngDoCheck'
5View renderedRenderComponent template renderedView updates
6Input properties changengOnChangesDetect new input changesLogs 'ngOnChanges'
7Change detection cyclengDoCheckCheck for changesLogs 'ngDoCheck'
8View updatedRenderUpdate template with new dataView updates
9Component destroyedngOnDestroyCleanup before removalLogs 'ngOnDestroy'
💡 Component lifecycle ends after ngOnDestroy when component is removed.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6After Step 7Final
Input propertiesundefinedset with initial valuesunchangedunchangedchangedchangedundefined after destroy
Component stateuninitializedinitializinginitializedcheckedcheckedcheckeddestroyed
Key Moments - 3 Insights
Why does ngOnChanges run before ngOnInit?
Because Angular sets input properties first and calls ngOnChanges to notify about these changes before running ngOnInit to initialize the component (see execution_table steps 2 and 3).
When does ngDoCheck run and why?
ngDoCheck runs during every change detection cycle after ngOnInit and after any input changes to allow custom change detection logic (see steps 4 and 7).
What happens if the component is destroyed?
Angular calls ngOnDestroy to let the component clean up resources before removal (see step 9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which hook runs immediately after the constructor?
AngOnChanges
BngOnInit
CngDoCheck
DngOnDestroy
💡 Hint
Check step 2 in the execution_table where ngOnChanges runs after constructor.
At which step does Angular render the component view for the first time?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at step 5 in execution_table where 'Render' happens after ngDoCheck.
If input properties change after initialization, which hooks run again?
Aconstructor and ngOnInit
BngOnChanges and ngDoCheck
CngOnDestroy only
DngOnInit only
💡 Hint
See steps 6 and 7 where ngOnChanges and ngDoCheck run on input changes.
Concept Snapshot
Angular lifecycle hooks run in this order:
constructor -> ngOnChanges (on inputs) -> ngOnInit -> ngDoCheck -> Render
On input changes: ngOnChanges -> ngDoCheck -> Render
On destroy: ngOnDestroy
ngOnChanges runs before ngOnInit to catch input changes early.
ngDoCheck runs every change detection cycle for custom checks.
Full Transcript
This visual execution shows Angular component lifecycle order. First, the constructor runs when the component is created. Then Angular sets input properties and calls ngOnChanges to notify about changes. Next, ngOnInit runs to initialize the component. After that, ngDoCheck runs for custom change detection logic. The component view is rendered. When input properties change later, ngOnChanges and ngDoCheck run again followed by view update. Finally, when the component is removed, ngOnDestroy runs to clean up. Variables like input properties and component state change accordingly during these steps.