Bird
0
0

Why is it important to implement ngOnDestroy in components that subscribe to observables?

hard📝 Conceptual Q10 of 15
Angular - Lifecycle Hooks
Why is it important to implement ngOnDestroy in components that subscribe to observables?
ATo automatically refresh data when component is destroyed
BTo prevent memory leaks by unsubscribing when component is destroyed
CTo delay component destruction until subscriptions complete
DTo restart subscriptions after component destruction
Step-by-Step Solution
Solution:
  1. Step 1: Understand subscription lifecycle

    Subscriptions keep resources active; if not unsubscribed, they cause memory leaks.
  2. Step 2: Role of ngOnDestroy

    ngOnDestroy is used to unsubscribe from observables to free resources when component is destroyed.
  3. Final Answer:

    To prevent memory leaks by unsubscribing when component is destroyed -> Option B
  4. Quick Check:

    Unsubscribe in ngOnDestroy to avoid leaks [OK]
Quick Trick: Unsubscribe in ngOnDestroy to prevent leaks [OK]
Common Mistakes:
  • Ignoring unsubscription causing memory leaks
  • Thinking ngOnDestroy refreshes or restarts subscriptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes