Skip to main content

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

  1. ngOnChanges(changes) - called when input properties change (@Input).
  2. ngOnInit() - called once after the first initialization of input properties.
  3. ngDoCheck() - called on every change detection cycle.
  4. ngAfterContentInit() - called after content is inserted (<ng-content>), if the directive is used on a component with content projection.
  5. ngAfterContentChecked() - called after every check of the inserted content.
  6. 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.