Angular Change Detection
Learn Angular Change Detection, including Zone.js, Default and OnPush strategies, the change detection tree, immutable updates, Signals integration, ChangeDetectorRef (markForCheck, detectChanges, detach, reattach), zoneless Angular, performance optimization, enterprise architecture, common mistakes, and interview questions.
Learning Objectives
By the end of this lesson, you will understand:
- What Change Detection is.
- Why Angular needs Change Detection.
- The complete Angular rendering lifecycle.
- How Zone.js works.
- Default Change Detection Strategy.
OnPushChange Detection Strategy.- Change Detection Tree.
- Component Dirty Checking.
- Immutable vs Mutable Objects.
- How Signals integrate with Change Detection.
- How
ChangeDetectorRefworks. markForCheck().detectChanges().detach()andreattach().- Zoneless Angular.
- Change Detection optimization techniques.
- Performance bottlenecks.
- Enterprise best practices.
- Real-world architecture.
- Common mistakes.
- Advanced interview questions.
Introduction
Imagine you're building TechLearningPro.
A student clicks:
Complete Lesson
Immediately:
- Progress should become 72% → 76%
- Sidebar should update
- Progress bar should update
- Certificate eligibility should update
- Lesson button should become Completed
The developer never manually redraws the page.
Angular automatically updates the UI.
This process is called Change Detection.
What is Change Detection?
Change Detection is Angular's mechanism for keeping the UI synchronized with application state.
Whenever application data changes, Angular determines:
- What changed?
- Which component depends on it?
- Which DOM elements need updating?
Architecture:
Application State│▼Change Detection│▼Compare Values│▼Update DOM
Why Do We Need Change Detection?
Suppose:
username = 'Jagannath';
Template:
<h2>{{ username }}</h2>
Later:
username = 'Krishna';
Without Change Detection:
Browser↓Still ShowsJagannath
With Angular:
username Changes↓Angular Detects Change↓DOM Updated↓Krishna
Angular Rendering Lifecycle
When data changes:
User Action│▼Component State Changes│▼Change Detection Starts│▼Angular Checks Bindings│▼DOM Updates│▼Browser Paint
What Triggers Change Detection?
Angular normally starts Change Detection after events such as:
- Button clicks
- Keyboard input
- HTTP responses
- Timers (
setTimeout,setInterval) - Promise resolution
- Observable emissions
- Signal updates
Example:
Button Click↓Counter++↓Change Detection↓UI Updates
Zone.js
Historically Angular relied on Zone.js.
Zone.js monitors asynchronous operations.
Examples:
Click EventHTTP RequestPromiseTimerObservable
Architecture:
Browser Event↓Zone.js↓Angular↓Change Detection
Zone.js automatically informs Angular that something may have changed.
Example with Button Click
count++;
Flow:
Button Click↓Zone.js↓Angular↓Change Detection↓DOM Updated
The developer doesn't manually refresh the screen.
Default Change Detection Strategy
Every component uses Default strategy unless specified otherwise.
Example:
@Component({changeDetection:ChangeDetectionStrategy.Default})
Architecture:
Application↓Root Component↓Child↓Child↓Child↓Entire Tree Checked
Default strategy checks every component in the affected tree.
Example
Suppose:
App├── Header├── Dashboard├── Footer
A button inside Dashboard changes.
With Default strategy:
App↓Header Checked↓Dashboard Checked