Bird
0
0

What is the main purpose of the ngOnDestroy lifecycle hook in Angular?

easy📝 Conceptual Q11 of 15
Angular - Lifecycle Hooks
What is the main purpose of the ngOnDestroy lifecycle hook in Angular?
ATo handle user input events
BTo initialize component data when it is created
CTo run cleanup code when a component or directive is removed
DTo render the component template
Step-by-Step Solution
Solution:
  1. Step 1: Understand the role of ngOnDestroy

    The ngOnDestroy hook is called when Angular removes a component or directive from the view.
  2. Step 2: Identify its main use

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

    To run cleanup code when a component or directive is removed -> Option C
  4. Quick Check:

    ngOnDestroy = cleanup [OK]
Quick Trick: Remember: ngOnDestroy cleans up before component removal [OK]
Common Mistakes:
  • Confusing ngOnDestroy with ngOnInit
  • Thinking it initializes data
  • Assuming it handles user events

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes