Overview
Angular’s change detection system is responsible for synchronizing your application’s data model with its view. Understanding how it works is crucial for building performant applications.Change Detection Strategies
Angular provides two primary change detection strategies that control when and how components are checked for changes.Default (Eager) Strategy
TheDefault strategy (also known as Eager) checks the component eagerly during every change detection cycle.
The
Default strategy has been renamed to Eager in recent versions. Both names refer to the same behavior.OnPush Strategy
TheOnPush strategy only checks the component when:
- An input property reference changes
- An event originates from the component or its children
- Change detection is manually triggered
- An observable linked to the template emits a new value
Manual Change Detection
ChangeDetectorRef
UseChangeDetectorRef to manually control change detection in your components.
Key Methods
Performance Optimization
Immutable Data Patterns
When usingOnPush, work with immutable data to ensure change detection triggers correctly.
Avoiding Common Pitfalls
Change Detection with Signals
When using signals in your templates, Angular automatically tracks dependencies and triggers change detection efficiently.Signals work seamlessly with
OnPush change detection, automatically marking components for check when signal values change.Zone.js Integration
Angular uses Zone.js to automatically detect asynchronous operations and trigger change detection.Best Practices
Use OnPush
Set
OnPush strategy on presentational components to reduce unnecessary checks.Immutable Data
Use immutable data patterns to make change detection reliable and predictable.
TrackBy Functions
Always use
trackBy with *ngFor to minimize DOM operations.Avoid Heavy Computations
Move expensive calculations to pipes or pre-compute them in component logic.
