0
0
Angularframework~10 mins

Zone.js and automatic detection in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Zone.js and automatic detection
Start Angular App
Zone.js patches async APIs
Async task starts (e.g., click, timer)
Zone.js tracks task execution
Angular detects changes automatically
Angular updates the DOM
Async task ends
Wait for next async task
Zone.js wraps async tasks to track when they start and finish, letting Angular automatically detect changes and update the UI.
Execution Sample
Angular
button.addEventListener('click', () => {
  this.count++;
});
When the button is clicked, Zone.js detects the event and Angular updates the UI automatically.
Execution Table
StepAsync TaskZone.js ActionAngular Change DetectionDOM Update
1App startsZone.js patches async APIsNo change detection yetInitial DOM rendered
2User clicks buttonZone.js detects click event startNo change detection yetNo DOM update
3Click handler runsZone.js tracks task executionChange detection runs after handlerDOM reflects new count
4Click handler endsZone.js detects task endNo further change detectionDOM stable
5Wait for next async taskZone.js idleNo change detectionDOM unchanged
💡 No async tasks running, Angular waits for next event to trigger change detection
Variable Tracker
VariableStartAfter Click 1After Click 2Final
count0122
Key Moments - 2 Insights
Why does Angular update the UI without calling detectChanges() manually?
Because Zone.js tracks async tasks and automatically triggers Angular's change detection after they finish, as shown in steps 2 and 3 of the execution_table.
What happens if an async task is not patched by Zone.js?
Angular won't detect changes automatically for that task, so the UI won't update unless change detection is triggered manually. This is why Zone.js patches common async APIs (step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what triggers Angular's change detection?
AZone.js detecting the start of an async task
BZone.js detecting the end of an async task
CAngular manually calling detectChanges()
DDOM updating automatically without detection
💡 Hint
Check steps 2 and 3 where Angular runs change detection after Zone.js tracks task execution.
According to variable_tracker, what is the value of count after the second click?
A2
B1
C0
DUndefined
💡 Hint
Look at the 'After Click 2' column for the 'count' variable.
If Zone.js did not patch the click event, what would happen to Angular's automatic detection?
AAngular would still detect changes automatically
BAngular would never update the DOM
CAngular would require manual change detection calls
DZone.js would patch other async tasks instead
💡 Hint
Refer to key_moments about what happens if async tasks are not patched.
Concept Snapshot
Zone.js wraps async tasks like clicks and timers.
It tracks when tasks start and finish.
After tasks finish, Angular runs change detection automatically.
This updates the UI without manual calls.
If tasks aren't patched, manual detection is needed.
Full Transcript
Zone.js is a library Angular uses to watch asynchronous tasks such as clicks or timers. When the app starts, Zone.js patches these async APIs to track their execution. When a user clicks a button, Zone.js detects the start of this async event and tracks it. After the click handler runs and the task ends, Zone.js notifies Angular to run change detection. Angular then updates the DOM to reflect any changes, like incrementing a counter. This process happens automatically without the developer needing to call change detection manually. If an async task is not patched by Zone.js, Angular will not detect changes automatically, and manual detection calls are required.