What are lifecycle hooks in Angular?

Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Angular basics — a prerequisite for any developer role.

Answer

Angular lifecycle hooks are methods that Angular calls at specific points in a component's or directive's lifecycle, allowing you to respond to creation, change, and destruction events. Implement the corresponding interface for each hook. Order of execution: (1) ngOnChanges(changes: SimpleChanges): called when an @Input() property changes. Receives a SimpleChanges object with previous and current values. Called before ngOnInit and whenever inputs change. Implement OnChanges; (2) ngOnInit(): called once after the first ngOnChanges. Best place for initialization logic (fetch data, subscribe to route params). The component's inputs are available. Implement OnInit; (3) ngDoCheck(): called every change detection cycle — even when Angular doesn't detect changes itself. For custom change detection. Implement DoCheck; (4) ngAfterContentInit(): called once after Angular projects external content into the component view (). Implement AfterContentInit; (5) ngAfterContentChecked(): called after every check of projected content. Implement AfterContentChecked; (6) ngAfterViewInit(): called once after Angular initializes the component's view (and child views). Access DOM elements via @ViewChild here. Implement AfterViewInit; (7) ngAfterViewChecked(): called after every check of the component's view. Implement AfterViewChecked; (8) ngOnDestroy(): called just before Angular destroys the component. Clean up: unsubscribe from Observables, clear timers, remove event listeners. Implement OnDestroy. Most commonly used: ngOnInit (initialization) and ngOnDestroy (cleanup).

Common Mistake

Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex Angular answers easy to follow.