📖 Scenario: You are building a simple Angular component that listens to a timer observable. This timer emits a number every second. You want to show the current timer value in the component and clean up the subscription when the component is destroyed.
🎯 Goal: Create an Angular standalone component that subscribes to a timer observable on initialization, updates a displayed counter, and unsubscribes when the component is destroyed.
📋 What You'll Learn
Create a standalone Angular component named TimerComponent
Use Angular's timer observable from rxjs that emits every 1000 milliseconds
Subscribe to the timer observable inside the component lifecycle
Store the emitted value in a component property named count
Unsubscribe from the observable when the component is destroyed to avoid memory leaks
Display the current count value in the component template
💡 Why This Matters
🌍 Real World
Timers and observables are common in Angular apps for live updates, animations, or polling data.
💼 Career
Understanding observables and component lifecycle is essential for Angular developers to manage data streams and resource cleanup properly.
Progress0 / 4 steps
1
Set up the Angular component and import timer
Create a standalone Angular component named TimerComponent. Import Component from @angular/core and timer from rxjs. Initialize a public property count with 0.
Angular
Hint
Start by importing Component and timer. Then create a standalone component with a count property set to 0.
2
Create a subscription property and timer observable
Inside TimerComponent, create a private property named subscription to hold the subscription. Create a timer observable named timer$ that emits every 1000 milliseconds using timer(0, 1000).
Angular
Hint
Add a private property subscription to store the subscription. Create timer$ using timer(0, 1000) to emit immediately and then every second.
3
Subscribe to the timer observable and update count
In the ngOnInit() lifecycle method, subscribe to timer$. Update the count property with the emitted value inside the subscription callback.
Angular
Hint
Implement ngOnInit(). Inside it, subscribe to timer$ and update count with the emitted value.
4
Unsubscribe from timer observable on component destroy
Implement the ngOnDestroy() lifecycle method. Inside it, unsubscribe from subscription to clean up the observable subscription.
Angular
Hint
Implement ngOnDestroy() and call unsubscribe() on subscription to avoid memory leaks.
Practice
(1/5)
1. What is the main reason to subscribe to an Observable inside the ngOnInit lifecycle hook in an Angular component?
easy
A. To start listening to data streams when the component is initialized
B. To stop listening to data streams when the component is destroyed
C. To create a new Observable instance
D. To update the component's template directly
Solution
Step 1: Understand ngOnInit role
ngOnInit runs once when the component is created, ideal to start tasks like subscribing to Observables.
Step 2: Purpose of subscribing in ngOnInit
Subscribing here means the component begins listening to data streams as soon as it is ready.
Final Answer:
To start listening to data streams when the component is initialized -> Option A
Quick Check:
Subscribe in ngOnInit = start listening [OK]
Hint: Subscribe in ngOnInit to start data listening early [OK]
Common Mistakes:
Subscribing in ngOnDestroy instead of ngOnInit
Expecting subscription to stop automatically
Confusing subscription with Observable creation
2. Which of the following is the correct way to unsubscribe from an Observable in the ngOnDestroy lifecycle hook?
easy
A. this.subscription.unsubscribe();
B. this.subscription.subscribe();
C. this.subscription.complete();
D. this.subscription.destroy();
Solution
Step 1: Identify the unsubscribe method
Angular subscriptions have an unsubscribe() method to stop listening to Observables.
Step 2: Match method to lifecycle hook
In ngOnDestroy, calling unsubscribe() properly cleans up the subscription.
Final Answer:
this.subscription.unsubscribe(); -> Option A
Quick Check:
Unsubscribe method = unsubscribe() [OK]
Hint: Use unsubscribe() to stop Observable listening [OK]
Common Mistakes:
Using subscribe() instead of unsubscribe() to stop
A. Observable should not be assigned to a property
B. Subscription syntax is incorrect
C. Missing unsubscribe in ngOnDestroy causes memory leak
D. ngOnInit should not contain subscriptions
Solution
Step 1: Check subscription management
The code subscribes to an Observable but never unsubscribes, risking memory leaks.
Step 2: Lifecycle hook missing unsubscribe
Without ngOnDestroy to unsubscribe, the subscription stays active even after component destruction.
Final Answer:
Missing unsubscribe in ngOnDestroy causes memory leak -> Option C
Quick Check:
Always unsubscribe to avoid leaks [OK]
Hint: Always unsubscribe in ngOnDestroy to prevent leaks [OK]
Common Mistakes:
Thinking subscription syntax is wrong
Believing Observables can't be class properties
Assuming ngOnInit forbids subscriptions
5. You want to create an Angular component that listens to a user input Observable and updates a display value. The Observable may emit multiple times quickly. Which approach best manages the subscription lifecycle and avoids memory leaks?
hard
A. Use a global variable to store data and avoid subscribing
B. Subscribe in the constructor and never unsubscribe
C. Subscribe in ngOnInit without unsubscribing, relying on Angular to clean up
D. Subscribe in ngOnInit and unsubscribe in ngOnDestroy using a Subscription object
Solution
Step 1: Proper subscription management
Subscribing in ngOnInit ensures the component starts listening after creation.
Step 2: Unsubscribe in ngOnDestroy to prevent leaks
Unsubscribing cleans up resources when the component is removed, avoiding memory leaks.
Final Answer:
Subscribe in ngOnInit and unsubscribe in ngOnDestroy using a Subscription object -> Option D