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 (
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.