← Back to Angular, page 1

angular-0018 · Medium mid

What is change detection in Angular, how does OnPush differ from Default, and when does an OnPush component re-render?

Change detection is how Angular finds what changed and updates the DOM. With Default, every component is checked after each event. With OnPush, a component is checked only when an input reference changes, an event fires inside it, an async pipe or signal read in its template changes, or markForCheck is called.

Full answer

Change detection compares the data the templates use with what is on screen and updates the DOM where they differ. In an app with zone.js, Angular runs this after every browser task that could have changed data: a click, a timer, an HTTP response. Zone.js tells Angular that such a task finished.

With the Default strategy the whole tree of components is checked from the top each time. That is simple and correct, but wasteful for large screens.

ChangeDetectionStrategy.OnPush tells Angular to skip a component (and its children) unless a reason to check appears. The reasons are:

  1. An input binding got a new value by reference (!==). Mutating an object in place is not a change.
  2. An event listener in the component's own template ran, such as (click).
  3. The async pipe received a new value.
  4. A signal read in the template changed.
  5. Code called ChangeDetectorRef.markForCheck() or detectChanges().

So with OnPush you treat data as immutable. Instead of student.marks += 5, you write student = { ...student, marks: student.marks + 5 }. This is a good habit anyway, and it makes the checks cheap.

Signals make this easier. A component that reads signals in its template is updated when those signals change, even if it is OnPush. Writing components that work with OnPush is also the best preparation for zoneless apps. In Angular 20 a generated component is still Default unless you ask for OnPush, so set it yourself.

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

interface Student {
  name: string;
  marks: number;
}

@Component({
  selector: 'app-student-card',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `<p>{{ student().name }}: {{ student().marks }}</p>`,
})
export class StudentCardComponent {
  student = input.required<Student>();
}

@Component({
  selector: 'app-class-room',
  imports: [StudentCardComponent],
  template: `
    <app-student-card [student]="student" />
    <button (click)="mutate()">Mutate</button>
    <button (click)="replace()">Replace</button>
  `,
})
export class ClassRoomComponent {
  student: Student = { name: 'Asha', marks: 70 };

  // Same object reference: the OnPush child is not refreshed.
  mutate(): void {
    this.student.marks += 5;
  }

  // New object reference: the OnPush child is refreshed.
  replace(): void {
    this.student = { ...this.student, marks: this.student.marks + 5 };
  }
}

Angular 20.3. Click Mutate: the card stays at 70. Click Replace: the card updates. Angular warns nothing in the mutate case, which is the point of the example.

Key points

  • Default checks every component on each event; OnPush checks only when it has a reason.
  • OnPush triggers: new input reference, own event, async pipe, changed signal, `markForCheck`.
  • Mutating an object that was passed in does not update an OnPush child.
  • Immutable updates and signals work naturally with OnPush.

Common mistakes

  • Mutating an array or object passed to an OnPush child and wondering why the view is stale.
  • Calling `detectChanges()` everywhere to patch a stale view, instead of fixing the data flow.
  • Believing OnPush stops the whole app from updating. It only skips components that have no trigger.

Follow-ups

  • What does `markForCheck` do that `detectChanges` does not?
  • How does zone.js know when to start change detection?
  • How does a signal read in an OnPush template trigger an update?

Tips

  • List the five triggers. An interviewer often asks for all of them.
  • Use the object-mutation example. It is the classic OnPush bug.
  • Mention signals and zoneless as the direction Angular is going.

Evaluation rubric

  • Lists OnPush triggers correctly.
  • Explains why mutation breaks OnPush.
  • Connects OnPush to immutability and signals.