Observable in component lifecycle
📖 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
TimerComponentUse Angular's
timer observable from rxjs that emits every 1000 millisecondsSubscribe to the timer observable inside the component lifecycle
Store the emitted value in a component property named
countUnsubscribe 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