Bird
0
0

Given this Angular code snippet, what will happen after 1 second?

medium📝 component behavior Q13 of 15
Angular - Change Detection
Given this Angular code snippet, what will happen after 1 second?
constructor(private zone: NgZone) {
  setTimeout(() => {
    this.zone.run(() => {
      this.message = 'Updated!';
    });
  }, 1000);
}
AThe UI does not update because setTimeout is outside Angular zone.
BThe UI updates to show 'Updated!' automatically after 1 second.
CAn error occurs because run() cannot be used inside setTimeout.
DThe message changes but UI needs manual refresh.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze setTimeout with NgZone.run

    The setTimeout callback runs inside zone.run(), so Angular detects changes.
  2. Step 2: Understand UI update behavior

    Angular automatically updates the UI to show the new message after 1 second.
  3. Final Answer:

    The UI updates to show 'Updated!' automatically after 1 second. -> Option B
  4. Quick Check:

    NgZone.run triggers UI update = A [OK]
Quick Trick: NgZone.run inside async triggers UI update automatically [OK]
Common Mistakes:
MISTAKES
  • Thinking setTimeout runs outside Angular zone here
  • Believing manual UI refresh is needed
  • Assuming run() causes errors inside setTimeout

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes