← Back to Angular, page 6

angular-0118 · Medium mid

What is the difference between ChangeDetectorRef detectChanges, markForCheck, detach and reattach, and when would you use each?

markForCheck marks a component and its ancestors as dirty so the next normal change detection pass checks them, which is the usual fix for OnPush. detectChanges runs change detection for that component and its children right now. detach removes the component from checks until reattach is called.

Full answer

ChangeDetectorRef gives a component control over its own change detection.

markForCheck(): marks this view and all its parents up to the root as "dirty". It does not run anything by itself. The next change detection pass (started by zone.js, an event, or a signal) will check these views, even if they use OnPush. Use it when data changed outside Angular's knowledge for an OnPush component, for example in a callback from a third party library or after a mutation of an input object. It is cheap and safe.

detectChanges(): runs change detection immediately, for this view and its children only (not for parents). Use it when you need the DOM updated now, for example before measuring an element, or in a detached view. Calling it often in loops is expensive. In a view that is already being checked, it can cause confusing errors, so avoid calling it from inside lifecycle hooks of the same view.

detach(): removes the view from the change detection tree. Angular stops checking it on normal passes, so it never updates unless you call detectChanges() yourself. Use it for components with very frequent data (a live chart) where you want to update on your own schedule (for example every 500 ms). reattach() puts it back.

checkNoChanges(): development helper that verifies nothing changed since the last check. It is behind the ExpressionChangedAfterItHasBeenChecked error.

Signals change the picture. When a template reads a signal, Angular marks that view for check automatically when the signal changes, so markForCheck is rarely needed with signals, and OnPush plus signals is the recommended pattern. In zoneless apps, signals, markForCheck, events and async pipe schedule the next pass.

Rule of thumb: prefer signals or the async pipe, use markForCheck for rare outside changes, and use detectChanges and detach only for measured performance work.

typescript
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, inject } from '@angular/core';

@Component({
  selector: 'app-live-score',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `<p>Score: {{ score }}</p>`,
})
export class LiveScoreComponent {
  private readonly cdr = inject(ChangeDetectorRef);
  score = 0;

  // A value that changes outside Angular's knowledge, for an OnPush component.
  updateFromThirdParty(value: number): void {
    this.score = value;
    this.cdr.markForCheck();
  }

  // Take control: stop automatic checks, refresh on your own schedule.
  startManual(): void {
    this.cdr.detach();
  }

  refreshNow(value: number): void {
    this.score = value;
    this.cdr.detectChanges();
  }

  stopManual(): void {
    this.cdr.reattach();
  }
}

Angular 20.3, strict template check passes. A Karma test shows that an OnPush view ignores a plain field change, updates after markForCheck, and updates after detach and detectChanges.

Key points

  • `markForCheck` marks the view and ancestors dirty for the next pass. It is the standard OnPush fix.
  • `detectChanges` checks this view and its children immediately.
  • `detach` and `reattach` take a view out of automatic checks, and you control updates.
  • Signals and the async pipe mark views for you.

Common mistakes

  • Calling `detectChanges` everywhere to "fix" updates instead of fixing the data flow.
  • Using `markForCheck` in a loop or every frame.
  • Detaching a view and forgetting to update it.

Follow-ups

  • Why does mutating an input object not update an OnPush component?
  • What does `ExpressionChangedAfterItHasBeenChecked` mean?
  • How does zoneless change detection decide to run?

Tips

  • Describe the three in one line each: mark, run now, remove.
  • Say that `markForCheck` does not run detection.
  • Mention signals as the modern answer.

Evaluation rubric

  • Defines each method correctly, including scope.
  • Gives realistic use cases.
  • Links to OnPush, signals, and zoneless.