Skip to main content

Lifecycle, change detection and styling

Learn Lifecycle, change detection and styling (Module 4 · Components, Angular) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Component lifecycle and destruction • Every component has a life: it is created, shown, updated and destroyed. Angular calls lifecycle hooks at each stage, like a theatre calling the actor at the right cue. The most used: ngOnInit (once, after inputs are set; load data here), ngOnChanges (an input changed), ngAfterViewInit (view ready), and ngOnDestroy (cleanup). • Component destruction : when a component is removed (route change, @if false) Angular calls ngOnDestroy. Use it to unsubscribe, stop timers and remove listeners. Modern alternative: inject(DestroyRef).onDestroy(...) .

More lessons in Module 4 · Components