📖 Scenario: You are building a simple Angular component that starts a timer when it loads. To keep your app clean and avoid memory leaks, you need to stop the timer when the component is destroyed.
🎯 Goal: Create an Angular standalone component that starts a timer counting seconds on load and properly cleans up the timer using ngOnDestroy when the component is removed.
📋 What You'll Learn
Create a standalone Angular component named TimerComponent
Add a variable seconds initialized to 0
Start a timer using setInterval that increments seconds every second
Store the timer ID in a variable called timerId
Implement the ngOnDestroy lifecycle method to clear the timer using clearInterval(timerId)
💡 Why This Matters
🌍 Real World
Timers are common in apps for clocks, countdowns, or refreshing data. Cleaning up timers prevents app slowdowns and bugs.
💼 Career
Understanding Angular lifecycle hooks like ngOnDestroy is essential for building efficient, bug-free Angular applications.
Progress0 / 4 steps
1
Create the TimerComponent with seconds variable
Create a standalone Angular component named TimerComponent with a public variable seconds initialized to 0.
Angular
Hint
Use public seconds = 0; inside the class to create the variable.
2
Add timerId variable to store the interval ID
Add a public variable called timerId of type any to store the ID returned by setInterval.
Angular
Hint
Declare public timerId: any; inside the class.
3
Start the timer in the constructor to increment seconds
In the constructor of TimerComponent, start a timer using setInterval that increments seconds by 1 every 1000 milliseconds. Assign the returned ID to timerId.
Angular
Hint
Use setInterval(() => { this.seconds++ }, 1000) inside the constructor and assign it to this.timerId.
4
Implement ngOnDestroy to clear the timer
Implement the ngOnDestroy lifecycle method in TimerComponent that calls clearInterval(this.timerId) to stop the timer when the component is destroyed.
Angular
Hint
Import OnDestroy and implement ngOnDestroy() to call clearInterval(this.timerId).
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