Skip to content
Briebug

Developer Resource Center

Sharing our recent findings, expertise, and insights with the community.

How do I manually trigger change detection in Angular?

Angular JavaScript TypeScript Frontend by Updated 5 min read

Short answer

Inject ChangeDetectorRef and call markForCheck() to have Angular check an OnPush component on the next pass, or detectChanges() to check it and its children right away. In current Angular you rarely need either: store state in signals, and setting a signal that the template reads schedules the update for you, with OnPush or zoneless change detection.

export class StatusComponent {
  private cdr = inject(ChangeDetectorRef);
  label = 'idle';          // a plain field
  status = signal('idle'); // a signal

  onSavedOutsideAngular() {
    this.label = 'saved';
    this.cdr.markForCheck(); // check on the next pass
  }

  onSaved() {
    this.status.set('saved'); // the view updates on its own
  }
}

How do I manually trigger change detection in Angular?

First, why would I want to?

Angular’s default change detection strategy may work well for a hobby app or presentation demo but if you’re dealing with a large or enterprise application, you’re likely going to take a performance hit. That’s because by default Angular checks every component in the tree, and every expression in its template, on every change detection pass. Manual change detection is one way to limit that. It is not the best way.

How do I trigger change detection manually?

Inject ChangeDetectorRef into the component and call one of its methods:

  • markForCheck() marks the component and all of its ancestors as dirty, so they are checked on the next change detection pass even if they use OnPush. This is the call you want most of the time.
  • detectChanges() runs change detection on the component and its children immediately, synchronously.
  • detach() takes the component out of automatic checking until you call reattach(). While detached, it only updates when you call detectChanges() yourself.
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, inject } from '@angular/core';

@Component({
  selector: 'app-price-ticker',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `<p>{{ price }}</p>`,
})
export class PriceTickerComponent {
  private cdr = inject(ChangeDetectorRef);
  price = 0;

  // Called from a WebSocket callback or a third-party library.
  onPrice(value: number) {
    this.price = value;
    this.cdr.markForCheck();
  }
}

This kind of “manual” change detection is a good fit when you need full control. For instance, if you’re getting data from a websocket that fires many times a second, you may want to detach() and call detectChanges() on a timer to limit how often the view updates. If you don’t need that control, it over-complicates things.

Then what is the best way?

By far, the best and simplest way to get good performance from your Angular application is the OnPush change detection strategy combined with signals or immutable data. When OnPush is enabled, Angular skips the component unless something notifies it. Signals are the most precise notification: a signal read in a template tells Angular exactly which component to refresh when its value changes.

import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';

@Component({
  selector: 'app-cart-summary',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `<p>{{ count() }} items, {{ total() }}</p>`,
})
export class CartSummaryComponent {
  items = signal<{ price: number }[]>([]);
  count = computed(() => this.items().length);
  total = computed(() => this.items().reduce((sum, item) => sum + item.price, 0));

  add(item: { price: number }) {
    // A new array, not a mutation: signals compare by reference too.
    this.items.update((items) => [...items, item]);
  }
}

No ChangeDetectorRef is needed: setting the signal schedules the update. If you keep plain objects instead of signals, treat them as immutable and create a new object or array on every change (spread syntax works well), or keep the state in a store such as NgRx.

How does OnPush work?

Using OnPush, Angular will only check “when notified”. Here are the scenarios that notify:

1. An input reference has changed.

  • This means anytime a parent binds a new value to one of the component’s inputs (input() or @Input). Angular compares the object reference, not individual properties. For this reason, immutable data is the way to go. Each time you change your data, you make a copy with a new object reference.

2. The component or its children raise an event.

  • An event bound in the template, such as a click, marks the component for check.
  • An example:
import { ChangeDetectionStrategy, Component, output } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';

@Component({
  selector: 'app-item-form',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ReactiveFormsModule],
  template: `
    <form [formGroup]="form">
      <input formControlName="name" />
      <button type="button" (click)="saveForm.emit(form.getRawValue())">Save</button>
    </form>
  `,
})
export class ItemFormComponent {
  form = new FormGroup({ name: new FormControl('', { nonNullable: true }) });
  saveForm = output<{ name: string }>();
}

3. A signal read in the template changes.

  • {{ count() }}: setting or updating count marks the component for check.

4. A bound observable in the template emits.

  • {{ myObservable$ | async }}: the async pipe calls markForCheck() for you on every emission.

5. Manual

  • detectChanges() immediately runs change detection in that component and its children. markForCheck() marks the component and its ancestors to be checked on the next pass.
  • As we’ve already discussed, this isn’t the most elegant option and is probably best kept for edge cases, such as values set from callbacks Angular does not know about.

What changes in a zoneless application?

Angular has traditionally used Zone.js to run change detection after any browser event, timer or promise. Zoneless change detection (provideZonelessChangeDetection(), stable since Angular 20.2 and the default for new projects since Angular 21) drops Zone.js and runs change detection only when Angular is notified: by a signal the template reads, a template event, the async pipe, or markForCheck(). Code that changes a plain field from a setTimeout or a third-party callback will no longer update the view on its own. Move that state into a signal, or call markForCheck().

In Summary

Now you know the differences between default change detection and OnPush change detection in Angular, as well as how to trigger change detection manually with ChangeDetectorRef. While this is possible, it’s rarely the best solution when signals, immutable data and the async pipe are available.

If you’re sold on the OnPush change detection strategy and using the Angular CLI, you can set the component generator schematic to automatically set OnPush as the change detection strategy for newly generated components. Use this command: ng config schematics.@schematics/angular:component.changeDetection OnPush

To learn more, and see one of the best examples I’ve seen detailing the ramifications of change detection strategies, check out this article written by Minko Gechev.