Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Migrating from observables to signals in Angular
📖 Scenario: You have an Angular component that uses an observable to track a user's online status. You want to migrate this to use Angular signals for simpler and more reactive state management.
🎯 Goal: Refactor the component to replace the observable with a signal that updates the user's online status reactively.
📋 What You'll Learn
Create a user status observable with exact values
Add a signal to hold the online status
Subscribe to the observable to update the signal
Update the template to display the signal value
💡 Why This Matters
🌍 Real World
Many Angular apps use observables for state, but signals offer simpler, more efficient reactive updates. Migrating helps reduce boilerplate and improve readability.
💼 Career
Understanding signals and how to migrate from observables is valuable for modern Angular development and maintaining up-to-date codebases.
Progress0 / 4 steps
1
Create the user status observable
In the component class, create a variable called userStatus$ that is an observable emitting the boolean values true and false alternately using of(true, false) from rxjs.
Angular
Hint
Use of(true, false) to create the observable emitting true then false.
2
Add a signal to hold the online status
Import signal from @angular/core and create a signal called online initialized to false in the component class.
Angular
Hint
Use signal(false) to create a signal holding the initial value false.
3
Subscribe to the observable to update the signal
Inside the component class constructor, subscribe to userStatus$ and update the online signal with the emitted values.
Angular
Hint
Subscribe to userStatus$ and call this.online.set(status) inside the subscription.
4
Update the template to display the signal value
Modify the component template to display the current value of the online signal by calling it as a function inside the interpolation: {{ online() }}.
Angular
Hint
Use {{ online() }} in the template to show the signal's current value.
Practice
(1/5)
1. What is the main benefit of migrating from Observable to signal() in Angular?
easy
A. More complex code but better performance
B. Signals require manual subscription management
C. Simpler reactive data handling with automatic UI updates
D. Signals do not support reactive updates
Solution
Step 1: Understand the purpose of signals
Signals in Angular provide a simpler way to handle reactive data by automatically updating the UI when the data changes.
Step 2: Compare with observables
Observables require subscriptions and manual management, while signals reduce this complexity and improve performance.
Final Answer:
Simpler reactive data handling with automatic UI updates -> Option C
Quick Check:
Signals simplify reactivity = B [OK]
Hint: Signals auto-update UI, observables need subscriptions [OK]
Common Mistakes:
Thinking signals require manual subscriptions
Believing signals increase code complexity
Assuming signals do not update UI reactively
2. Which of the following is the correct way to create a signal in Angular?
easy
A. const count = createSignal(0);
B. const count = new Signal(0);
C. const count = observable(0);
D. const count = signal(0);
Solution
Step 1: Recall Angular signal creation syntax
Angular uses the signal() function to create reactive signals.
Step 2: Check options for correct syntax
Only const count = signal(0); matches Angular's official syntax.
Final Answer:
const count = signal(0); -> Option D
Quick Check:
Use signal() function = C [OK]
Hint: Use signal() function, not new or createSignal [OK]
Common Mistakes:
Using new Signal() instead of signal()
Confusing with createSignal() from other frameworks
Using observable() which is for RxJS
3. Given this Angular code migrating from observable to signal: