الانتقال إلى المحتوى
FrontendAngular· 7 دقائق للقراءة

OnPush Isn't 'Only When Inputs Change': What Really Triggers a Check

OnPush is a dirty flag with four triggers, and only one of them comes from the parent.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • Angular 22
محتويات المقال

An Angular 22 app opens an upload dialog with createComponent(UploadProgress) and pushes each progress value into the dialog's percent input. The dialog shows 0% and stays there until the upload finishes and it closes. A log in the subscription prints 10, 20, 30 up to 100, and ref.instance.percent holds each value. "I'm changing its input," the developer says, "and OnPush updates when inputs change."

The common misconception

The belief: an OnPush component updates when, and only when, one of its input references changes. Change an input, it re-renders; leave the inputs alone, it never will.

It's how OnPush was taught for years, usually as "OnPush plus immutable inputs", and the most famous OnPush bug seems to confirm it: push an item into an array you passed to an OnPush child and the child doesn't update, because the reference didn't change. The rule has two halves, and both are off. Inputs aren't watched at all; Angular marks the component when it sets one itself. And inputs are only one of four triggers, the only one that comes from the parent. In Angular 22, where every component is OnPush unless it says otherwise, the difference shows up everywhere.

What actually happens

OnPush is a dirty flag, not a watcher

During a pass, Angular walks the component tree. An component that isn't marked dirty is skipped, together with everything below it, except views that a signal marked on their own. One that is marked gets its template re-evaluated and becomes clean again, so the next pass skips it unless something marks it again. Four things mark it:

  • Angular sets one of its inputs, from a template binding or ComponentRef.setInput(), to a value that differs by Object.is.
  • An event is handled in its template or any descendant's: a DOM event binding, an output binding or @HostListener.
  • A signal its template reads changes.
  • Someone calls markForCheck() on it, which the async pipe does on every emission.

Only the first one comes from the parent. The other three come from inside, so an OnPush component with its own state updates just fine without any input changing:

TypeScript faq-item.ts
import { Component, input, signal } from '@angular/core';@Component({  selector: 'app-faq-item',  template: `    <button (click)="open.update((o) => !o)">{{ question() }}</button>    @if (open()) { <p>{{ answer() }}</p> }  `,})export class FaqItem {  readonly question = input.required<string>();  readonly answer = input.required<string>();  readonly open = signal(false); // the click and the signal both mark FaqItem}

The click handler marks FaqItem, and the open signal its template reads marks it too. No input changed, no immutable copy was needed, no ChangeDetectorRef was injected.

Writing a property isn't setting an input

Back to the dialog. ref.instance.percent = 40 is an ordinary property write on a JavaScript object. Angular didn't perform it, so nothing marks the view. The docs list this edge case: changing an @Input() property through a reference you got from @ViewChild or @ContentChild doesn't trigger change detection for an OnPush component, and a ComponentRef from createComponent() is no different. setInput() is the way in from code. It sets the input through Angular, marks the view and, in a zoneless app, schedules the pass:

TypeScript upload-dialogs.ts
import { Service, ViewContainerRef } from '@angular/core';import { Observable } from 'rxjs';@Service()export class UploadDialogs {  open(host: ViewContainerRef, progress$: Observable<number>): void {    const ref = host.createComponent(UploadProgress);    progress$.subscribe((percent) => {      ref.instance.percent = percent; // a plain write: nothing is marked    });  }}

Here UploadProgress still declares @Input() percent = 0. Had it used a signal input, percent = input(0), the wrong version wouldn't even compile: a signal input is read-only, so setInput() is the only way to change it from outside.

Marks travel up, skips travel down

When an event fires in a component, Angular marks that component and every ancestor up to the root, because a pass can only reach a view through its parents. The docs' example: a click in an OnPush LoginComponent also gets its OnPush parent MainComponent checked, since the child is part of the parent's view. That has a cost. A keystroke in a small filter box nested inside a heavy OnPush grid re-evaluates the grid's whole template on every key. Signals are the precise trigger: Angular refreshes the component whose template read the signal and only walks through its OnPush ancestors to reach it, without re-evaluating their templates.

Skipping works the other way: when a pass skips an OnPush component, it skips its whole subtree. The API docs put it plainly: the OnPush strategy applies to all child directives and cannot be overridden.

What happensIs the OnPush component checked?
A parent's binding produces a new value (Object.is differs)Yes
Code writes ref.instance.x = ... or a @ViewChild propertyNo: Angular didn't set it
Code calls ref.setInput('x', ...)Yes
A click or output is handled in it or in a descendantYes, together with its ancestors
A signal its template reads changesYes
An async pipe in its template receives a valueYes: the pipe calls markForCheck()

How to apply it in your project

  1. Find writes to other components' inputsSearch for ref.instance. and for assignments through @ViewChild or @ContentChild references. Replace each one with ref.setInput(), a template binding or a shared signal.
  2. Keep internal state inside, as signalsDon't lift a component's own state into inputs, or wrap it in new objects, just to make OnPush notice it. A signal or a template event already marks the component.
  3. Pass new values, not mutated onesWhen a parent changes data it passes down, create a new array or object, so the binding produces a value that differs by Object.is.
  4. Watch the hot pathsEvents mark every ancestor, so keep high-frequency interactions, like typing or dragging, in small components that aren't nested inside your heaviest templates.

Key takeaways

  • OnPush is a dirty flag: a pass checks the component only if something marked it since the last check.
  • Four things mark it: a new input set by Angular, an event in it or below it, a signal its template reads, and markForCheck().
  • Assigning an input property from code isn't setting an input; use setInput() or a binding.
  • Marks travel up to the root, and a skipped OnPush component takes its whole subtree with it, Eager children included.

📚 المصادر

  1. Skipping component subtrees — Angular
  2. ChangeDetectionStrategy — Angular API
  3. ChangeDetectorRef — Angular API
  4. Signals: reading signals in OnPush components — Angular
  5. Angular without ZoneJS (Zoneless) — Angular

التعليقات (0)