Problem Statement
What are Angular lifecycle hooks and when do you use them?
Explanation
Lifecycle hooks let you run logic at key times: `ngOnChanges` (on @Input changes), `ngOnInit` (once after first inputs), `ngDoCheck` (custom change detection), `ngAfterContentInit/Checked` (projected content), `ngAfterViewInit/Checked` (view/child view ready), and `ngOnDestroy` (cleanup). Typical use: fetch data in `ngOnInit`, unsubscribe in `ngOnDestroy`.
Code Solution
SolutionRead Only
export class TestComponent implements OnInit, OnDestroy {
sub?: Subscription;
ngOnInit(){ /* start fetching */ }
ngOnDestroy(){ this.sub?.unsubscribe(); }
}