Challenge - 5 Problems
Manual Change Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when ChangeDetectorRef.detectChanges() is called?
Consider an Angular component where you manually call
detectChanges() from ChangeDetectorRef. What is the immediate effect on the component's view?Angular
import { Component, ChangeDetectorRef } from '@angular/core'; @Component({ selector: 'app-manual-detect', template: `<p>{{counter}}</p><button (click)="increment()">Increment</button>` }) export class ManualDetectComponent { counter = 0; constructor(private cdr: ChangeDetectorRef) {} increment() { this.counter++; this.cdr.detectChanges(); } }
Attempts:
2 left
💡 Hint
Think about what detectChanges() does in Angular's change detection mechanism.
✗ Incorrect
Calling detectChanges() triggers Angular to check the component and its children immediately, updating the view to reflect any changes in data bindings.
❓ lifecycle
intermediate2:00remaining
When should you use ApplicationRef.tick() in Angular?
Which scenario correctly describes when to use
ApplicationRef.tick() to trigger change detection manually?Attempts:
2 left
💡 Hint
ApplicationRef.tick() affects the whole app, not just one component.
✗ Incorrect
ApplicationRef.tick() triggers change detection for the entire Angular application immediately, useful in rare cases when you need a full manual check.
🔧 Debug
advanced2:00remaining
Why does calling detectChanges() inside ngAfterViewInit cause ExpressionChangedAfterItHasBeenCheckedError?
Given this Angular component code, why does calling
detectChanges() inside ngAfterViewInit cause an ExpressionChangedAfterItHasBeenCheckedError?Angular
import { Component, ChangeDetectorRef, AfterViewInit } from '@angular/core'; @Component({ selector: 'app-error-demo', template: `<p>{{message}}</p>` }) export class ErrorDemoComponent implements AfterViewInit { message = 'Initial'; constructor(private cdr: ChangeDetectorRef) {} ngAfterViewInit() { this.message = 'Changed after view init'; this.cdr.detectChanges(); } }
Attempts:
2 left
💡 Hint
Think about Angular's change detection timing and when it expects data to be stable.
✗ Incorrect
The error occurs because Angular detects that a bound value changed after it finished checking the view, which is not allowed to prevent inconsistent UI.
📝 Syntax
advanced2:00remaining
Which code snippet correctly injects ChangeDetectorRef and triggers manual detection?
Select the code snippet that correctly injects
ChangeDetectorRef and triggers manual change detection in an Angular component method.Attempts:
2 left
💡 Hint
Remember how to use dependency injection and call instance methods.
✗ Incorrect
Option D correctly injects ChangeDetectorRef as a private property and calls detectChanges() on that instance.
❓ state_output
expert3:00remaining
What is the output after manually detaching and reattaching change detection?
Given this Angular component code, what will be the displayed counter value after clicking the button twice?
Angular
import { Component, ChangeDetectorRef } from '@angular/core'; @Component({ selector: 'app-detach-demo', template: `<p>{{counter}}</p><button (click)="increment()">Increment</button>` }) export class DetachDemoComponent { counter = 0; constructor(private cdr: ChangeDetectorRef) { this.cdr.detach(); } increment() { this.counter++; if (this.counter === 2) { this.cdr.reattach(); } } }
Attempts:
2 left
💡 Hint
Consider what happens when change detection is detached and then reattached.
✗ Incorrect
Initially, change detection is detached, so the view does not update on the first click (counter=1). After the second click, reattach() is called, so the view updates to show 2.