Angular & Signals (283 Qs)•Part 1: Architecture, Components, Directives & Lifecycle (1 - 35)
What are lifecycle hooks available?
Angular application goes through an entire set of processes or has a lifecycle right from its initiation to the end of the application. The representation of lifecycle in pictorial representation as follows,

The description of each lifecycle method is as below,
- ngOnChanges: When the value of a data bound property changes, then this method is called.
- ngOnInit: This is called whenever the initialization of the directive/component after Angular first displays the data-bound properties happens.
- ngDoCheck: This is for the detection and to act on changes that Angular can't or won't detect on its own.
- ngAfterContentInit: This is called in response after Angular projects external content into the component's view.
- ngAfterContentChecked: This is called in response after Angular checks the content projected into the component.
- ngAfterViewInit: This is called in response after Angular initializes the component's views and child views.
- ngAfterViewChecked: This is called in response after Angular checks the component's views and child views.
- ngOnDestroy: This is the cleanup phase just before Angular destroys the directive/component.
Practical Code Example
typescript
import { Component, OnInit, OnChanges, OnDestroy, SimpleChanges } from '@angular/core';
@Component({ selector: 'app-lifecycle', standalone: true, template: `<p>Lifecycle Demo</p>` })
export class LifecycleComponent implements OnInit, OnChanges, OnDestroy {
ngOnChanges(changes: SimpleChanges): void { console.log('1. ngOnChanges'); }
ngOnInit(): void { console.log('2. ngOnInit - Initialize component state / API calls'); }
ngDoCheck(): void { console.log('3. ngDoCheck - Custom change detection'); }
ngAfterContentInit(): void { console.log('4. ngAfterContentInit'); }
ngAfterViewInit(): void { console.log('5. ngAfterViewInit - View & Child DOM ready'); }
ngOnDestroy(): void { console.log('6. ngOnDestroy - Clean up subscriptions & timers'); }
}