Bird
0
0

Identify the issue in this Angular component's ngOnDestroy method:

medium📝 Debug Q6 of 15
Angular - Lifecycle Hooks
Identify the issue in this Angular component's ngOnDestroy method:
export class CleanupComponent implements OnDestroy {
  private timerId: any;

  ngOnDestroy() {
    clearInterval(timerId);
  }
}
AclearInterval should be replaced with clearTimeout
BThe variable <code>timerId</code> is not referenced with <code>this</code> inside ngOnDestroy
CngOnDestroy should return a value
DThe component should implement OnInit instead of OnDestroy
Step-by-Step Solution
Solution:
  1. Step 1: Check variable usage

    Inside class methods, instance variables must be accessed with this.
  2. Step 2: Analyze the code

    The code calls clearInterval(timerId) but timerId is not defined locally; it should be this.timerId.
  3. Step 3: Correct usage

    Use clearInterval(this.timerId) to properly clear the interval.
  4. Final Answer:

    The variable timerId is not referenced with this inside ngOnDestroy -> Option B
  5. Quick Check:

    Use this.timerId to access instance variable [OK]
Quick Trick: Always prefix instance variables with this. inside methods [OK]
Common Mistakes:
  • Omitting 'this' when accessing class properties
  • Confusing clearInterval with clearTimeout
  • Expecting ngOnDestroy to return a value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes