What is the purpose of the ngOnDestroy lifecycle hook in Angular?
The ngOnDestroy hook is used to perform cleanup just before Angular destroys a component or directive. It helps free resources like subscriptions or timers to prevent memory leaks.
Click to reveal answer
beginner
When exactly is ngOnDestroy called in an Angular component?
ngOnDestroy is called right before Angular removes the component or directive from the DOM, such as when navigating away or conditionally hiding the component.
Click to reveal answer
intermediate
Why should you unsubscribe from Observables inside ngOnDestroy?
Unsubscribing inside ngOnDestroy stops ongoing Observable streams to avoid memory leaks and unexpected behavior after the component is gone.
Click to reveal answer
intermediate
Show a simple example of using ngOnDestroy to clear a timer.
Inside ngOnDestroy, call clearInterval or clearTimeout to stop timers started in the component, preventing them from running after destruction.
Click to reveal answer
beginner
What happens if you forget to implement cleanup in ngOnDestroy?
If cleanup is missed, resources like subscriptions or timers keep running, causing memory leaks and bugs because the component is no longer active but still consuming resources.
Click to reveal answer
What is the main use of ngOnDestroy in Angular?
ATo handle user input events
BTo initialize component data
CTo render the component template
DTo clean up resources before component destruction
✗ Incorrect
ngOnDestroy is specifically for cleanup tasks before Angular destroys the component.
When should you unsubscribe from Observables in Angular?
AInside <code>ngOnDestroy</code>
BInside <code>ngOnInit</code>
CInside the constructor
DNever unsubscribe
✗ Incorrect
Unsubscribing in ngOnDestroy ensures no memory leaks after the component is destroyed.
Which Angular lifecycle hook is called just before a component is removed from the DOM?
AngOnDestroy
BngAfterViewInit
CngOnChanges
DngDoCheck
✗ Incorrect
ngOnDestroy runs right before the component is removed.
What could happen if you forget to clear timers in ngOnDestroy?
ATimers stop automatically
BTimers keep running causing unexpected behavior
CAngular throws an error
DNothing happens
✗ Incorrect
Timers continue running and may cause bugs or memory leaks.
Which of these is NOT a reason to use ngOnDestroy?
ATo unsubscribe from Observables
BTo clear intervals or timeouts
CTo fetch data from a server
DTo release resources
✗ Incorrect
ngOnDestroy is for cleanup, not for fetching data.
Explain why and how you use ngOnDestroy in an Angular component.
Think about what happens when a component is removed and what resources might still be running.
You got /4 concepts.
Describe a real-life example where forgetting to implement ngOnDestroy cleanup could cause problems.
Imagine a component that keeps listening to data even after you leave its page.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the ngOnDestroy lifecycle hook in Angular?
easy
A. To handle user input events
B. To initialize component data when it is created
C. To run cleanup code when a component or directive is removed
D. To render the component template
Solution
Step 1: Understand the role of ngOnDestroy
The ngOnDestroy hook is called when Angular removes a component or directive from the view.
Step 2: Identify its main use
It is used to clean up resources like timers, subscriptions, or event listeners to avoid memory leaks.
Final Answer:
To run cleanup code when a component or directive is removed -> Option C
Quick Check:
ngOnDestroy = cleanup [OK]
Hint: Remember: ngOnDestroy cleans up before component removal [OK]
Common Mistakes:
Confusing ngOnDestroy with ngOnInit
Thinking it initializes data
Assuming it handles user events
2. Which of the following is the correct way to implement ngOnDestroy in an Angular component?
easy
A. destroy() { console.log('Component destroyed'); }
B. ngOnDestroy = () => { console.log('Component destroyed'); }
C. function ngOnDestroy() { console.log('Component destroyed'); }
D. ngOnDestroy() { console.log('Component destroyed'); }
Solution
Step 1: Recall Angular lifecycle hook syntax
Angular lifecycle hooks are methods inside the component class with exact names like ngOnDestroy().
Step 2: Check method declaration style
The correct syntax is a method, not an arrow function or standalone function, and must be named exactly ngOnDestroy.
Final Answer:
ngOnDestroy() { console.log('Component destroyed'); } -> Option D