Change Detection is one of Angular's most critical internal mechanisms, responsible for updating the DOM whenever your application's state changes. A clear understanding of how it works can help you write more efficient and performant Angular apps.
What is Change Detection?
Change Detection is the process by which Angular synchronizes the DOM with the component's data model. Every time data changes in your application, Angular runs change detection to determine what needs to be updated in the view.
How Angular Triggers Change Detection
Angular triggers change detection in various scenarios, including:
- User interactions (clicks, typing, etc.)
- HTTP responses or timers (setTimeout, setInterval)
- Observable subscriptions
- Component or input property changes
Angular runs change detection starting from the root component and traverses the component tree to check for updates.
Zone.js and Its Role
Angular uses a library called Zone.js to intercept asynchronous operations. It patches browser APIs and ensures Angular knows when to run change detection.
Example:
setTimeout(() => {
this.value = 'updated'; // Angular runs change detection after this
}, 1000);
The Change Detection Tree
Angular builds a tree of components and runs change detection from the top (root) to the leaves:
- Each component is checked to see if any bindings have changed.
- Angular updates the view only if changes are detected.
Change Detection Strategies
Angular provides two strategies:
Default
- Checks every component in the tree.
- Suitable for most use cases.
OnPush
- Only checks a component when:
- One of its input properties changes.
- An event is triggered inside the component.
- Helps improve performance in large apps.
Usage:
@Component({
selector: 'app-optimized',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `{{ data.name }}`
})
export class OptimizedComponent {
@Input() data!: { name: string };
}
Detecting and Marking Changes Manually
You can manually trigger change detection using ChangeDetectorRef
:
constructor(private cdr: ChangeDetectorRef) {}
updateValue() {
this.value = 'manually updated';
this.cdr.detectChanges();
}
Or mark the component to be checked in the next cycle:
this.cdr.markForCheck();
Common Pitfalls
- Mutating objects directly: Angular doesn’t detect changes if you mutate an object without changing its reference.
- Too many change detections: Avoid triggering change detection too frequently.
- Inefficient templates: Heavy or complex templates can slow down the detection process.
Best Practices
- Use
OnPush
strategy where possible. - Keep your templates simple and clean.
- Avoid complex logic in templates.
- Prefer immutable data structures for better performance with OnPush.
Conclusion
Angular's Change Detection is powerful but can impact performance if misunderstood. By mastering how it works under the hood and applying best practices, you can create highly responsive and scalable Angular applications.
What’s your favorite trick for optimizing change detection? Share your thoughts below! 🚀
Top comments (4)
Great breakdown of Angular's change detection mechanism! Understanding Zone.js and the OnPush strategy is crucial for optimizing performance.
On my macOS setup, I use ServBay to manage various development environments, which simplifies testing Angular's change detection strategies in different scenarios.
Thank you so much for your feedback! 🙌 I totally agree — grasping how Zone.js works and when to apply the OnPush strategy can really elevate app performance.
Also, thanks for mentioning ServBay — I haven’t tried it before, but it sounds like an interesting tool for managing dev environments on macOS. I’ll definitely look into it and see how it could fit into my workflow for testing Angular behavior. Appreciate the tip! 👍
How in the world you can write about Angular change detection in 2025 without talking about signals??
Angular 18 shows zoneless way.
This post focuses on Angular’s traditional change detection (Zone.js, OnPush) because that’s still widely used in many current codebases and relevant for understanding how Angular has historically worked.
Signals are not the same thing as change detection — they’re a tool that plugs into a new detection strategy. Signals can opt in to notify Angular exactly what changed, removing the need for broad checks via Zone.js.
The old model (Zone.js + OnPush) is still fully supported and widely used in real-world Angular apps. It tells Angular: “Hey, something changed — run change detection across the tree.”
If you want know more welcome to post about signals.