Bird
0
0

Why should Angular developers implement the ngOnDestroy lifecycle hook in their components?

easy📝 Conceptual Q1 of 15
Angular - Lifecycle Hooks
Why should Angular developers implement the ngOnDestroy lifecycle hook in their components?
ATo handle user input events during component interaction
BTo initialize component data after the view is rendered
CTo release resources like subscriptions and timers before the component is removed
DTo define the component's template and styles
Step-by-Step Solution
Solution:
  1. Step 1: Understand ngOnDestroy's role

    The ngOnDestroy hook is called just before Angular destroys the component.
  2. Step 2: Purpose of ngOnDestroy

    It is used to clean up resources such as subscriptions, timers, or event listeners to prevent memory leaks.
  3. Final Answer:

    To release resources like subscriptions and timers before the component is removed -> Option C
  4. Quick Check:

    Cleanup before destruction [OK]
Quick Trick: Use ngOnDestroy to clean resources before component removal [OK]
Common Mistakes:
  • Confusing ngOnDestroy with initialization hooks like ngOnInit
  • Assuming Angular automatically cleans up subscriptions
  • Using ngOnDestroy to handle user events

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes