0
0
Angularframework~10 mins

Component lifecycle overview in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Component lifecycle overview
Component Creation
Constructor runs
ngOnChanges if inputs
ngOnInit runs
Component Rendered
User Interaction or Input Changes
ngOnChanges/ngDoCheck
ngAfterContentInit/ngAfterContentChecked
ngAfterViewInit/ngAfterViewChecked
ngOnDestroy runs
Component Destroyed
This flow shows the main steps Angular components go through from creation to destruction, including lifecycle hooks triggered at each stage.
Execution Sample
Angular
import { Component, OnInit, OnDestroy } from '@angular/core';

@Component({ selector: 'app-sample', template: '<p>Hello</p>' })
export class SampleComponent implements OnInit, OnDestroy {
  ngOnInit() { console.log('Init'); }
  ngOnDestroy() { console.log('Destroy'); }
}
A simple Angular component that logs messages when it initializes and when it is destroyed.
Execution Table
StepLifecycle HookTrigger/EventActionConsole Output
1ConstructorComponent createdInitialize component instance
2ngOnChangesInput properties setRespond to input changes
3ngOnInitAfter first ngOnChangesComponent initialization logicInit
4Component RenderAfter ngOnInitRender template to DOM
5User InteractionUser changes inputDetect changes
6ngOnChangesInput changedRespond to input changes
7ngDoCheckChange detection cycleCustom change detection
8ngAfterContentInitAfter content projectionContent init logic
9ngAfterContentCheckedAfter content checkedContent check logic
10ngAfterViewInitAfter view initView init logic
11ngAfterViewCheckedAfter view checkedView check logic
12ngOnDestroyJust before component destroyedCleanup logicDestroy
13-Component removedComponent destroyed and removed from DOM
💡 Component lifecycle ends after ngOnDestroy when component is removed from DOM.
Variable Tracker
VariableConstructorAfter ngOnInitAfter User Input ChangeAfter ngOnDestroy
componentInstanceCreatedInitializedUpdated if inputs changedDestroyed
Key Moments - 3 Insights
Why does ngOnInit run only once and not on every input change?
ngOnInit runs once after the first ngOnChanges to initialize the component. Later input changes trigger ngOnChanges but not ngOnInit, as shown in execution_table rows 3 and 6.
What is the difference between ngOnChanges and ngDoCheck?
ngOnChanges runs only when input properties change, while ngDoCheck runs every change detection cycle for custom checks. See execution_table rows 6 and 7.
When should cleanup code be placed in the component?
Cleanup code should be in ngOnDestroy, which runs just before the component is removed, as shown in execution_table row 12.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the component first render its template?
AStep 3 - ngOnInit
BStep 4 - Component Render
CStep 1 - Constructor
DStep 12 - ngOnDestroy
💡 Hint
Check the 'Action' column for when the template is rendered to the DOM.
According to variable_tracker, what happens to componentInstance after ngOnDestroy?
AIt remains initialized
BIt is updated with new inputs
CIt is destroyed
DIt is recreated
💡 Hint
Look at the 'After ngOnDestroy' column for componentInstance.
If input properties change, which lifecycle hook runs according to execution_table?
AngOnChanges
BngOnInit
CngOnDestroy
DngAfterViewInit
💡 Hint
See which hook runs on input changes in execution_table rows 2 and 6.
Concept Snapshot
Angular component lifecycle:
- Constructor: create instance
- ngOnChanges: on input changes
- ngOnInit: once after first inputs
- Render: template shown
- ngDoCheck: every change detection
- ngAfterContentInit/ViewInit: after content/view setup
- ngOnDestroy: cleanup before removal
Use hooks to manage component behavior at each stage.
Full Transcript
This visual execution shows the Angular component lifecycle from creation to destruction. It starts with the constructor creating the component instance. Then ngOnChanges runs if input properties are set or changed. ngOnInit runs once after the first ngOnChanges to initialize the component. The component template is then rendered to the DOM. User interactions or input changes trigger ngOnChanges and ngDoCheck for detecting changes. After content and view are initialized and checked, the component continues running until it is destroyed. ngOnDestroy runs cleanup code before the component is removed from the DOM. Variables like the component instance change state accordingly during these steps. Key moments include understanding when ngOnInit runs, the difference between ngOnChanges and ngDoCheck, and placing cleanup in ngOnDestroy. The quizzes test knowledge of render timing, component destruction, and input change hooks.