Bird
0
0

Identify the issue in this ngDoCheck implementation:

medium📝 Debug Q7 of 15
Angular - Lifecycle Hooks
Identify the issue in this ngDoCheck implementation:
ngDoCheck() {
  if (this.items !== this.previousItems) {
    this.previousItems = this.items;
    console.log('Items changed');
  }
}
AIt only detects changes if the array reference changes, missing internal mutations
BIt causes an infinite loop by updating previousItems inside ngDoCheck
CIt throws an error because previousItems is not initialized
DIt incorrectly compares array lengths instead of references
Step-by-Step Solution
Solution:
  1. Step 1: Understand reference comparison

    The condition compares array references, not contents.
  2. Step 2: Detecting internal mutations

    Mutations like push/pop do not change the reference, so changes are missed.
  3. Final Answer:

    It only detects changes if the array reference changes, missing internal mutations -> Option A
  4. Quick Check:

    Reference check misses internal changes [OK]
Quick Trick: Reference check misses internal changes [OK]
Common Mistakes:
  • Assuming internal mutations change reference
  • Expecting ngDoCheck to detect deep changes automatically
  • Not initializing previousItems before use

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes