0
0
Angularframework~30 mins

Why observables matter in Angular - See It in Action

Choose your learning style9 modes available
Why observables matter in Angular
📖 Scenario: You are building a simple Angular app that shows live updates of a user's score in a game. The score changes over time, and you want the app to update the display automatically whenever the score changes.
🎯 Goal: Build a small Angular component that uses an observable to track the user's score and updates the displayed score automatically when it changes.
📋 What You'll Learn
Create a signal to hold the initial score
Create an observable that emits new score values
Subscribe to the observable and update the signal when a new score arrives
Display the current score in the component template
💡 Why This Matters
🌍 Real World
Many apps need to show live data like scores, notifications, or sensor readings. Observables let Angular handle these updates efficiently and reactively.
💼 Career
Understanding observables and signals is key for Angular developers to build responsive, maintainable apps that handle asynchronous data streams.
Progress0 / 4 steps
1
Set up the initial score signal
Create a signal called score and set its initial value to 0 inside the Angular component.
Angular
Need a hint?

Use signal(0) to create a reactive value that starts at zero.

2
Create an observable to emit new scores
Import interval from rxjs and map from rxjs/operators. Create an observable called scoreUpdates$ that emits a new score every second, increasing by 1 each time.
Angular
Need a hint?

Use interval(1000) to emit every second and map to add 1 to each emitted number.

3
Subscribe to the observable and update the signal
In the component constructor, subscribe to scoreUpdates$ and update the score signal with each new value.
Angular
Need a hint?

Use subscribe on the observable and call this.score.set(newScore) inside the callback.

4
Complete the component template for accessibility
Add an aria-live="polite" attribute to the <h1> tag in the template to announce score changes to screen readers.
Angular
Need a hint?

Add aria-live="polite" inside the <h1> tag to help screen readers announce updates.