Discover how signals and observables make your app smarter and easier to build!
Signal vs observable comparison in Angular - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a web app where you want to update the user interface whenever data changes, like showing live scores or chat messages.
You try to do this by manually checking for changes and updating the screen yourself.
Manually tracking every change is tiring and easy to mess up.
You might forget to update some parts or update too often, making the app slow or buggy.
Signals and observables help by automatically tracking changes and updating only what needs to change.
Signals give you a simple way to hold and react to a single value, while observables handle streams of values over time.
let score = 0; // Manually update UI every time score changes function updateScore(newScore) { score = newScore; document.getElementById('score').textContent = score; }
import { signal } from '@angular/core'; const scoreSignal = signal(0); // UI updates automatically when scoreSignal changes scoreSignal.set(10);
You can build apps that react instantly and efficiently to data changes without writing complex update logic.
In a live sports app, scores update in real-time and the UI changes smoothly without you writing extra code to track every update.
Manual updates are slow and error-prone.
Signals track single values simply and update UI automatically.
Observables handle streams of data over time for complex scenarios.
Practice
Solution
Step 1: Understand what a signal represents
Signals hold a single reactive value that updates the UI automatically when changed.Step 2: Compare with observable behavior
Observables handle streams of data over time and require subscriptions, unlike signals.Final Answer:
A signal holds a single reactive value and updates UI automatically. -> Option CQuick Check:
Signal = single reactive value [OK]
- Thinking signals handle multiple async events like observables
- Believing signals require subscriptions
- Confusing signals with HTTP request handlers
Solution
Step 1: Recall Angular signal creation syntax
Signals are created using the signal() function with an initial value.Step 2: Identify incorrect options
Observable creation uses new Observable(), subscribe is a method, and createObservable() is not valid.Final Answer:
const count = signal(0); -> Option BQuick Check:
signal() creates signals [OK]
- Using new Observable() to create a signal
- Confusing subscribe() with signal creation
- Using non-existent createObservable() function
const count = signal(1); count.set(5); console.log(count());
Solution
Step 1: Understand signal value update
The signal is created with initial value 1, then updated to 5 using set().Step 2: Check the value returned by calling the signal
Calling count() returns the current value, which is 5 after set().Final Answer:
5 -> Option DQuick Check:
Signal value after set() = 5 [OK]
- Assuming initial value remains after set()
- Thinking signals cannot be updated
- Confusing signal() call with observable subscription
const obs = new Observable(subscriber => {
subscriber.next(1);
});
obs.next(2);Solution
Step 1: Understand Observable instance methods
Observable instances do not have a next() method; next() is called on the subscriber inside the constructor.Step 2: Identify misuse of next() outside subscriber
Calling obs.next(2) is invalid and causes an error.Final Answer:
Observables do not have a next() method on the instance. -> Option AQuick Check:
next() is on subscriber, not observable instance [OK]
- Trying to call next() on observable instance
- Confusing signal() with observable creation
- Believing subscription is needed before next()
Option A: Use a signal to hold the counter value. Option B: Use an observable and subscribe to updates. Option C: Use a Promise to fetch the counter value. Option D: Use a BehaviorSubject without subscription.
Solution
Step 1: Identify the requirement for simple immediate UI update
A simple counter state that updates UI immediately fits the signal use case.Step 2: Compare other options
Observable requires subscription and is better for streams; Promise resolves once; BehaviorSubject needs subscription to update UI.Final Answer:
Signal is best because it holds a single reactive value and updates UI automatically. -> Option AQuick Check:
Simple state + auto UI update = signal [OK]
- Choosing observable for simple state without subscription
- Using Promise for reactive UI updates
- Assuming BehaviorSubject updates UI without subscription
