📖 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
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
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
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
Hint
Add aria-live="polite" inside the <h1> tag to help screen readers announce updates.
Practice
(1/5)
1. Why are observables important in Angular applications?
easy
A. They help handle asynchronous data streams smoothly.
B. They replace all HTML elements with custom tags.
C. They make Angular apps run faster by compiling code.
D. They automatically style components without CSS.
Solution
Step 1: Understand what observables do
Observables allow Angular to listen and react to data that changes over time, like user input or server responses.
Step 2: Compare options to observable purpose
Only "They help handle asynchronous data streams smoothly." correctly describes observables as tools for handling asynchronous data streams smoothly.
Final Answer:
They help handle asynchronous data streams smoothly. -> Option A
Quick Check:
Observables = handle async data [OK]
Hint: Observables manage changing data over time [OK]
Common Mistakes:
Thinking observables style components
Confusing observables with HTML tags
Believing observables speed up compilation
2. Which of the following is the correct way to subscribe to an observable in Angular?
easy
A. observable.watch(data => console.log(data));
B. observable.listen(data => console.log(data));
C. observable.on(data => console.log(data));
D. observable.subscribe(data => console.log(data));
Solution
Step 1: Recall Angular observable syntax
In Angular, the method to receive data from an observable is called subscribe.
Step 2: Check each option's method name
Only observable.subscribe(data => console.log(data)); uses the correct method subscribe. Others like listen, on, and watch are not valid observable methods.
Final Answer:
observable.subscribe(data => console.log(data)); -> Option D
B. Multiple subscriptions can cause repeated HTTP calls.
C. Observables cannot be assigned to variables.
D. The HTTP get method does not return an observable.
Solution
Step 1: Analyze multiple subscriptions to the same observable
Each subscription to an HTTP observable triggers a new HTTP request, causing repeated calls.
Step 2: Identify the problem in the code
Here, this.data$ is subscribed twice, so two HTTP requests are sent, which is inefficient and often unwanted.
Final Answer:
Multiple subscriptions can cause repeated HTTP calls. -> Option B
Quick Check:
Multiple subscriptions = repeated HTTP calls [OK]
Hint: Avoid multiple subscriptions to HTTP observables [OK]
Common Mistakes:
Thinking subscribe is misspelled
Believing observables can't be variables
Assuming HTTP get returns non-observable
5. You want to create a component that updates its display every second with the current time using observables. Which approach correctly uses observables to achieve this in Angular?
hard
A. Use interval(1000).subscribe(time => this.currentTime = new Date()) inside ngOnInit and unsubscribe on destroy.
B. Use setTimeout inside the template to update time every second.
C. Call new Date() once in the constructor and display it.
D. Use a loop inside the template to refresh the time continuously.
Solution
Step 1: Identify observable usage for timed updates
The interval observable emits values at set intervals, perfect for updating time every second.
Step 2: Proper subscription and cleanup
Subscribing in ngOnInit and unsubscribing on destroy prevents memory leaks and keeps updates smooth.
Final Answer:
Use interval(1000).subscribe(time => this.currentTime = new Date()) inside ngOnInit and unsubscribe on destroy. -> Option A