Angular Life Cycle

 Angular LifeCycle:
Angular Components go through a sequence of lifecycle hooks from creation to destruction. These hooks let you run code at specific moments.

Lifecycle Flow:
                                           Constructor()
                                                    |
                                          ngOnChanges()
                                                    |
                                             ngOnInit()
                                                    |
                                           ngDoCheck()
                                                    |
                                         ngAfterContentInit()
                                                    |
                                    ngAfterContentChecked()
                                                    |
                                         ngAfterViewInit()
                                                    |
                                        ngAfterViewChecked()
                                                    |
                                           ngOnDestroy()

Contructor():

It initialize class, component . it runs first and heavy logic is avoided here .
Mostly used for inject services or 
dependency


ngOnChanges():

It is triggered when input properties change(@Input)
It detect and triggered when data received from parent component changes.
Parent Compoent

@Input(): is decorator that allows a child component to receive data from its parent compoents.

eg: Parent HTML <app-child [username]="parentName"></app-child>

child: @Input() username:strings

ngOnInit():
It run once after angular initializes the compoent.
it runs after constructor if ngOnChange is not detected, when the components are ready to use.
Mostly used for API Calls, setting Defaults values, subscribing to services and preparing data for an templates.

Syntax:    ngOnInit(){
                    
                    }

ngOnCheck():
It runs whenever angular runs change detection. 
it runs very freqently, even when no @Input( )reference changes.
it used for custom change detection logic and  for more control an can affect performance.

When to use:
Detech changes in nested objects
custom comparision logi
when ngOnChanges() is not enough

when not to use:
API calls
Heavy Calculation
Simple Input updates

ngAfterContentInit():
it runs once after angular projects external content into a component.
it run code after parent content is inserted into the child.

ngAfterContentChecked():
it runs everytime angular check projected content inside a component.
it runs after angular has checked and updated content coming from the parent component.


when it runs
runs multiple times during change detection
runs whenever projected content is checked/updated.

ngAfterViewInit():
It runs once after angular initializes the component's view and its child views.
it runs when the template and all child components are fully loaded and ready.

Access DOM elements using @ViewChild
initialize third party librares
Manipulate UI after rendering.


ngAfterViewChecked()
it runs after angular checks the component's view and all child views.
it runs every time angular verifies or updates the UI

Uses:
Debugging view updates, tracking UI changes and rare cases of manual DOM synchronization,

ngOnDestroy():
it runs just before a component is removed destroy from DOM.

uses:
Unsubscribing from observalbles.
clear timers or intervals
remove event listeners

Comments

Popular posts from this blog

Angular Guides Notes

SIGNALS

Directives