Skip to main content

How does ChangeDetectionStrategy affect the hooks?

In Angular, ChangeDetectionStrategy determines when Angular checks the component for changes, and this affects the calling of the lifecycle hooks related to change detection:

Key points

  1. Strategies:
  • Default - the standard strategy. Angular checks the component and all its child components on any event, timer, or data change.
  • OnPush - an optimized strategy. Angular checks the component only when its input properties (@Input) change or on events inside the component.
  1. Effect on the hooks:
  • ngOnChanges() - called only when the input properties change, as usual.
  • ngDoCheck(), ngAfterContentChecked(), ngAfterViewChecked() - with OnPush, they can fire less often, because Angular does not run a change check on the component without an explicit change to the input data or an event.
  • The other hooks (ngOnInit(), ngAfterContentInit(), ngAfterViewInit(), ngOnDestroy()) - always called in the usual order, regardless of the strategy.
  1. Conclusion:
  • With OnPush, some change-detection hooks can fire less often, which improves performance but requires care when working with data that does not arrive through @Input.

In other words, ChangeDetectionStrategy affects how often the change-detection hooks are called, but not the initialization and destruction hooks.

Short Answer

Interview ready
Premium

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