Angular

Angular OnPush Change Detection: What Triggers a Check, and Where Signals Fit

Angular keeps the DOM in sync with component state through change detection. With the Default strategy, every component in the tree is checked after any event Angular knows about: a click anywhere, a timer, an HTTP response. Most of the time that's cheap. In screens with many components, such as dashboards, long lists or data grids, those checks add up and show as long "Angular" tasks in the browser's Performance panel.

ChangeDetectionStrategy.OnPush lets a component opt out of most of those checks. This guide shows exactly what triggers an OnPush component to be checked, demonstrates the most common bug it causes (mutated inputs that never render), and explains how signals change the picture in Angular 17 and later.

What OnPush Actually Changes

An OnPush component (and the subtree below it) is skipped during change detection unless it has been marked dirty. Angular marks it dirty when:

  1. Any @Input() receives a new value. Angular compares with ===, so a new primitive or a new object/array reference counts, but mutating an existing object does not.
  2. An event handler bound in its template runs, for example a (click) inside the component or one of its children.
  3. The async pipe in its template receives a new value from an Observable or Promise.
  4. A signal read in its template changes (Angular 17+).
  5. You call ChangeDetectorRef.markForCheck() yourself.

Marking a component dirty also marks its ancestors, so the path from the root down to it gets checked on the next pass. Nothing else in a skipped subtree is touched.

OnPush is a performance tool, and it doesn't make Angular reactive. If a value changes in a way none of the five triggers can see, the view simply stays stale.

A Minimal Demo

The examples use standalone components and inline templates so they work in any recent Angular version without depending on CLI file names.

TypeScript
// counter-display.component.ts
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';

@Component({
  selector: 'app-counter-display',
  standalone: true,
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <p>Count: {{ state.count }}</p>
    <p>Checked {{ logCheck() }} times</p>
  `,
})
export class CounterDisplayComponent {
  @Input({ required: true }) state!: { count: number };
  private checks = 0;

  // Called each time Angular checks this template. Fine for a demo, never in real code.
  logCheck() {
    return ++this.checks;
  }
}
TypeScript
// app.component.ts
import { Component } from '@angular/core';
import { CounterDisplayComponent } from './counter-display.component';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CounterDisplayComponent],
  template: `
    <p>Parent ticks: {{ ticks }}</p>
    <button (click)="replace()">Replace object</button>
    <button (click)="mutate()">Mutate object</button>
    <app-counter-display [state]="state" />
  `,
})
export class AppComponent {
  ticks = 0;
  state = { count: 0 };

  constructor() {
    // Triggers change detection every second, unrelated to the child's input.
    setInterval(() => this.ticks++, 1000);
  }

  replace() {
    this.state = { count: this.state.count + 1 }; // new reference: child updates
  }

  mutate() {
    this.state.count++; // same reference: the OnPush child does NOT update
  }
}

Run it and watch the "Checked" counter:

  • Without OnPush, it climbs every second, because the parent's timer causes a full-tree check. In development mode it climbs by two per tick, since Angular runs an extra verification pass.
  • With OnPush, it stays still while the timer runs. It only moves when you click Replace object.
  • Clicking Mutate object changes the data, but the child keeps showing the old count. The input reference didn't change, so the child was never marked dirty. The new value appears the next time something else marks it, which looks like a random delayed update to users.

Fixing Stale Views

Prefer new references over mutation

Treat inputs as immutable and replace objects and arrays instead of editing them:

TypeScript
// Instead of: this.items.push(item);
this.items = [...this.items, item];

// Instead of: this.user.name = name;
this.user = { ...this.user, name };

Use the async pipe for Observables

The async pipe subscribes, unsubscribes on destroy, and calls markForCheck() when a value arrives:

HTML
@if (user$ | async; as user) {
  <app-user-card [user]="user" />
}

Subscribing manually in the component and assigning to a field doesn't trigger anything in an OnPush component unless you call markForCheck() yourself.

Use signals for local and shared state

Signals are the most direct fit for OnPush. When a template reads a signal, Angular records that dependency and marks the component dirty when the signal changes:

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

@Component({
  selector: 'app-cart-summary',
  standalone: true,
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <p>{{ itemCount() }} items, total {{ total() }}</p>
    <button (click)="add(250)">Add item</button>
  `,
})
export class CartSummaryComponent {
  private prices = signal<number[]>([]);
  itemCount = computed(() => this.prices().length);
  total = computed(() => this.prices().reduce((sum, p) => sum + p, 0));

  add(price: number) {
    this.prices.update(list => [...list, price]);
  }
}

Signal inputs (input() instead of @Input()) work the same way. Because only components that read a changed signal get marked, signals are also what makes zoneless change detection practical (provideZonelessChangeDetection(), stable since Angular 20).

markForCheck() as a last resort

For values that change outside every trigger above, for example a callback from a third-party library, inject ChangeDetectorRef and call markForCheck() after updating state:

TypeScript
constructor(private cdr: ChangeDetectorRef) {}

ngOnInit() {
  this.chart.on('select', point => {
    this.selected = point;
    this.cdr.markForCheck();
  });
}

detectChanges() also exists, but it runs a synchronous check of this component's subtree immediately. Reach for it only when you need the DOM updated before the current function continues. In most cases, updating a signal is cleaner than either method.

⚠️ Common mistake: Adding OnPush to existing components without checking how they receive data. Components that mutate shared objects or update fields from manual subscriptions will start showing stale values. Convert leaf, presentational components first, where inputs are simple and easy to audit, and work upwards.

Is It Worth It?

Measure before and after. In Chrome DevTools, record a Performance profile of the interaction that feels slow, or use the Angular DevTools profiler, which shows how long each change detection pass took and which components were checked. If most of the time is spent in a few expensive templates (large @for loops without a good track expression, or heavy function calls in bindings), fixing those may matter more than switching strategies.

Summary

  • OnPush components are only checked when an input reference changes, a template event fires, the async pipe emits, a template signal changes, or you call markForCheck().
  • Mutating an object passed as an input is the classic OnPush bug. Pass new references instead.
  • Signals and computed() fit OnPush naturally and are the foundation for zoneless Angular.
  • Use markForCheck() only for updates that come from outside Angular's view, such as third-party callbacks.
Advertisement