0
0
Angularframework~20 mins

Triggering detection manually in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Manual Change Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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();
  }
}
AThe component's view updates only after Angular's next automatic change detection cycle.
BThe component's view updates immediately to reflect the new counter value.
CThe component's view does not update until the component is destroyed and recreated.
DCalling detectChanges() causes an error because it cannot be called manually.
Attempts:
2 left
💡 Hint
Think about what detectChanges() does in Angular's change detection mechanism.
lifecycle
intermediate
2:00remaining
When should you use ApplicationRef.tick() in Angular?
Which scenario correctly describes when to use ApplicationRef.tick() to trigger change detection manually?
AWhen you want to reset the component's state to initial values.
BWhen you want to detect changes only in a single component and its children.
CWhen you want to trigger change detection for the entire application immediately.
DWhen you want to stop Angular's automatic change detection.
Attempts:
2 left
💡 Hint
ApplicationRef.tick() affects the whole app, not just one component.
🔧 Debug
advanced
2: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();
  }
}
ABecause the view was already checked and changing data after that triggers the error.
BBecause ngAfterViewInit runs before the component is initialized.
CBecause message is not a valid property to bind in the template.
DBecause detectChanges() cannot be called inside lifecycle hooks.
Attempts:
2 left
💡 Hint
Think about Angular's change detection timing and when it expects data to be stable.
📝 Syntax
advanced
2: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.
A
constructor(private cdr: ChangeDetectorRef) {}

update() {
  this.cdr.tick();
}
B
constructor(cdr: ChangeDetectorRef) {}

update() {
  cdr.detectChanges();
}
C
constructor(private cdr: ChangeDetectorRef) {}

update() {
  ChangeDetectorRef.detectChanges();
}
D
constructor(private cdr: ChangeDetectorRef) {}

update() {
  this.cdr.detectChanges();
}
Attempts:
2 left
💡 Hint
Remember how to use dependency injection and call instance methods.
state_output
expert
3: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();
    }
  }
}
AAfter two clicks, the displayed counter jumps directly to 2 without showing 1.
BThe displayed counter updates immediately on every click.
CThe displayed counter never updates because change detection is detached.
DAfter two clicks, the displayed counter is 1, then updates to 2 after reattach.
Attempts:
2 left
💡 Hint
Consider what happens when change detection is detached and then reattached.