0
0
Angularframework~10 mins

When to use OnPush in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - When to use OnPush
Component Initialized
Change Detection Triggered
OnPush Strategy?
NoRun Full Change Detection
Yes
Check Input References Changed?
NoSkip Change Detection
Yes
Run Change Detection for Component
Update View
This flow shows how Angular decides to run change detection with OnPush: it only runs if input references change or an event occurs.
Execution Sample
Angular
import { Component, ChangeDetectionStrategy, Input } from '@angular/core';

@Component({
  selector: 'app-child',
  template: '{{data.name}}',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ChildComponent {
  @Input() data: { name: string };
}
A child component using OnPush change detection that updates only when its input reference changes.
Execution Table
StepTriggerInput Reference Changed?Change Detection Run?View Updated?
1Component initializedN/AYes (initial)Yes
2Parent updates input with same objectNoNoNo
3Parent updates input with new objectYesYesYes
4User event inside componentN/AYesYes
5No changesNoNoNo
💡 Change detection stops when input references do not change and no events occur.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5
inputReferenceChangedN/AN/ANoYesN/ANo
changeDetectionRunYesYesNoYesYesNo
viewUpdatedYesYesNoYesYesNo
Key Moments - 2 Insights
Why doesn't the view update when the parent changes the input object but keeps the same reference?
Because OnPush only runs change detection when the input reference changes, not when the object's internal properties change. See execution_table row 2.
Does OnPush run change detection on user events inside the component?
Yes, user events trigger change detection even with OnPush. See execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the input reference change causing change detection to run?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Input Reference Changed?' column in the execution_table.
If the parent updates the input object by mutating its property but keeps the same reference, what happens to the view?
AView updates
BChange detection runs fully
CView does not update
DComponent re-initializes
💡 Hint
See execution_table row 2 where input reference is unchanged.
Which event always triggers change detection in OnPush components?
AInput reference unchanged
BUser event inside component
CNo changes
DParent does nothing
💡 Hint
Look at execution_table row 4 for user event trigger.
Concept Snapshot
Angular OnPush Strategy:
- Runs change detection only when input references change or events occur.
- Skips checks if inputs are unchanged for better performance.
- Use OnPush for components with immutable inputs or event-driven updates.
- Helps avoid unnecessary view updates.
- Requires careful input management.
Full Transcript
This visual execution shows how Angular's OnPush change detection strategy works. When a component uses OnPush, Angular runs change detection only if the input reference changes or if a user event happens inside the component. If the parent changes the input object's properties but keeps the same reference, OnPush skips updating the view. This improves performance by avoiding unnecessary checks. User events inside the component always trigger change detection. Understanding this helps you decide when to use OnPush for better app speed and predictable updates.