Bird
0
0

This Angular component does not update the view after a timer:

medium📝 Debug Q7 of 15
Angular - Change Detection
This Angular component does not update the view after a timer:
constructor(private zone: NgZone) {
this.zone.runOutsideAngular(() => {
setTimeout(() => {
this.counter++;
}, 1000);
});
}

How to fix it so the UI updates correctly?
AWrap the counter increment inside this.zone.run() inside setTimeout
BRemove runOutsideAngular and use run() instead
CAll of the above
DCall detectChanges() manually after increment
Step-by-Step Solution
Solution:
  1. Step 1: Identify why UI doesn't update

    runOutsideAngular skips change detection, so counter++ alone won't update UI.
  2. Step 2: Explore fixes

    Wrapping increment in run() re-enters Angular zone; removing runOutsideAngular runs inside zone; manual detectChanges triggers update.
  3. Final Answer:

    All of the above -> Option C
  4. Quick Check:

    Any method re-entering Angular zone or triggering detection updates UI [OK]
Quick Trick: Re-enter Angular zone or trigger detection to update UI [OK]
Common Mistakes:
MISTAKES
  • Thinking runOutsideAngular code updates UI automatically
  • Ignoring manual detectChanges option
  • Assuming only one fix works

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes