Which lifecycle hooks do directives support?
Directives in Angular support almost the same lifecycle hooks as components, except for a few related to the view template (since a directive has no template of its own).
Main hooks for directives
ngOnChanges(changes)- called when input properties change (@Input).ngOnInit()- called once after the first initialization of input properties.ngDoCheck()- called on every change detection cycle.ngAfterContentInit()- called after content is inserted (<ng-content>), if the directive is used on a component with content projection.ngAfterContentChecked()- called after every check of the inserted content.ngOnDestroy()- called before the directive is removed from the element, to clean up resources and unsubscribe from events.
Conclusion: directives have almost the full set of component lifecycle hooks, but the view-related hooks (
ngAfterViewInit,ngAfterViewChecked) apply only to components, since a directive has no template of its own.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.