Zone.js helps Angular know when to update the screen automatically. It watches for changes in your app without you needing to tell it.
Zone.js and automatic detection in Angular
Introduction
Syntax
Angular
Zone.current.run(() => {
// your code here
});Zone.js patches async operations like events, timers, and promises to detect changes.
Angular uses Zone.js internally, so you usually don't need to call it directly.
Examples
Angular
setTimeout(() => { console.log('Timer done'); }, 1000);
Angular
Zone.current.run(() => {
console.log('Run inside zone');
});Sample Program
This Angular component shows a counter and a button. When you click the button, a timer runs for 1 second. Zone.js detects the timer and automatically updates the counter on the screen without extra code.
Angular
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <h1>Counter: {{ counter }}</h1> <button (click)="startTimer()">Start Timer</button> ` }) export class AppComponent { counter = 0; startTimer() { setTimeout(() => { this.counter++; }, 1000); } }
Important Notes
You usually don't need to interact with Zone.js directly in Angular apps.
If you run code outside Angular's zone, the view won't update automatically.
You can use NgZone service to run code inside or outside the Angular zone if needed.
Summary
Zone.js helps Angular know when to update the screen automatically.
It watches async tasks like timers and events without extra code.
This makes your app update smoothly and easily.
Practice
1. What is the main role of
Zone.js in Angular?easy
Solution
Step 1: Understand Zone.js purpose
Zone.js tracks asynchronous operations like timers and events.Step 2: Connect to Angular UI updates
When async tasks finish, Zone.js tells Angular to update the screen automatically.Final Answer:
It automatically detects async tasks and triggers UI updates. -> Option DQuick Check:
Zone.js auto-detects async tasks = A [OK]
Hint: Zone.js watches async tasks to update UI automatically [OK]
Common Mistakes:
- Confusing Zone.js with routing or styling
- Thinking Zone.js compiles code
- Assuming manual UI updates are needed
2. Which syntax correctly shows how Angular uses Zone.js to detect changes?
easy
Solution
Step 1: Identify Angular's Zone.js API
Angular providesNgZone.run()to run code inside Angular's zone for UI updates.Step 2: Check other options
Other options are not valid Angular Zone.js methods.Final Answer:
<code>NgZone.run(() => { /* update UI */ })</code> -> Option CQuick Check:
NgZone.run triggers UI update = B [OK]
Hint: NgZone.run wraps code to trigger Angular updates [OK]
Common Mistakes:
- Using non-existent Zone.js methods
- Confusing runOutsideAngular with run
- Assuming detectChanges is a Zone.js method
3. Given this Angular code snippet, what will happen after 1 second?
constructor(private zone: NgZone) {
setTimeout(() => {
this.zone.run(() => {
this.message = 'Updated!';
});
}, 1000);
}medium
Solution
Step 1: Analyze setTimeout with NgZone.run
The setTimeout callback runs insidezone.run(), so Angular detects changes.Step 2: Understand UI update behavior
Angular automatically updates the UI to show the new message after 1 second.Final Answer:
The UI updates to show 'Updated!' automatically after 1 second. -> Option BQuick Check:
NgZone.run triggers UI update = A [OK]
Hint: NgZone.run inside async triggers UI update automatically [OK]
Common Mistakes:
- Thinking setTimeout runs outside Angular zone here
- Believing manual UI refresh is needed
- Assuming run() causes errors inside setTimeout
4. What is wrong with this Angular code that tries to update UI?
constructor(private zone: NgZone) {
this.zone.runOutsideAngular(() => {
setTimeout(() => {
this.message = 'Changed!';
}, 500);
});
}medium
Solution
Step 1: Understand runOutsideAngular effect
Code insiderunOutsideAngulardoes not trigger Angular change detection.Step 2: Analyze setTimeout inside runOutsideAngular
The message changes but Angular does not detect it, so UI won't update automatically.Final Answer:
UI will not update because changes happen outside Angular zone. -> Option AQuick Check:
runOutsideAngular skips UI update = C [OK]
Hint: runOutsideAngular blocks automatic UI updates [OK]
Common Mistakes:
- Assuming setTimeout is disallowed inside runOutsideAngular
- Thinking UI updates automatically anyway
- Believing this code throws errors
5. You want to optimize an Angular app by running heavy calculations without triggering UI updates, then update UI once done. Which approach using Zone.js is correct?
hard
Solution
Step 1: Use runOutsideAngular for heavy work
Heavy calculations insiderunOutsideAngular()avoid triggering UI updates repeatedly.Step 2: Use run to update UI once done
After calculations, callzone.run()to update UI and trigger change detection.Final Answer:
Run calculations inside zone.runOutsideAngular(), then update UI inside zone.run(). -> Option AQuick Check:
Heavy work outside, UI update inside = D [OK]
Hint: Heavy work outside zone, UI update inside zone.run [OK]
Common Mistakes:
- Running heavy work inside Angular zone causing slow UI
- Updating UI outside Angular zone so no refresh
- Doing both heavy work and UI update outside zone
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
