
OnPush Isn't 'Only When Inputs Change': What Really Triggers a Check
- كاتب موثّق
- Angular 22
محتويات المقال
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
What actually happens
OnPush is a dirty flag, not a watcher
Angular sets one of its inputs, from a template binding or ComponentRef.setInput(), to a value that differs byObject.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 theasyncpipe does on every emission.
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}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
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:
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 }); }}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.setInput('percent', percent); // marks the view, schedules a pass }); }}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
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.
Object.is differs) | |
ref.instance.x = ... or a @ViewChild property | |
ref.setInput('x', ...) | |
async pipe in its template receives a value | markForCheck() |
How to apply it in your project
Find writes to other components' inputs Search for ref.instance.and for assignments through@ViewChildor@ContentChildreferences. Replace each one withref.setInput(), a template binding or a shared signal.Keep internal state inside, as signals Don'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. Pass new values, not mutated ones When a parent changes data it passes down, create a new array or object, so the binding produces a value that differs by Object.is.Watch the hot paths Events 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.
التعليقات (0)