Signals and observables both help manage changing data in Angular apps. They let your app react when data updates, but they work in different ways.
Signal vs observable comparison in Angular
Start learning this pattern below
Jump into concepts and practice - no test required
import { signal } from '@angular/core'; import { Observable } from 'rxjs'; // Signal example const count = signal(0); // Observable example const count$ = new Observable(subscriber => { subscriber.next(0); subscriber.complete(); });
Signals hold a single value and update automatically when changed.
Observables represent streams of values over time and require subscription.
set and read it by calling it like a function.import { signal } from '@angular/core'; const name = signal('Alice'); name.set('Bob'); console.log(name());
import { of } from 'rxjs'; const name$ = of('Alice'); name$.subscribe(value => console.log(value));
update which takes a function to change the current value.import { signal } from '@angular/core'; const counter = signal(0); counter.update(c => c + 1); console.log(counter());
import { interval } from 'rxjs'; const timer$ = interval(1000); timer$.subscribe(val => console.log(val));
This Angular component shows a signal and an observable count side by side. The signal updates only when you click the button. The observable count updates every second automatically.
import { Component, signal } from '@angular/core'; import { interval } from 'rxjs'; @Component({ selector: 'app-root', template: ` <h2>Signal count: {{ count() }}</h2> <h2>Observable count: {{ observableCount }}</h2> <button (click)="incrementSignal()">Increment Signal</button> ` }) export class AppComponent { count = signal(0); observableCount = 0; constructor() { interval(1000).subscribe(val => { this.observableCount = val; }); } incrementSignal() { this.count.update(c => c + 1); } }
Signals are simpler and built into Angular for state tracking.
Observables are more powerful for complex async streams but need subscriptions.
Use signals for local state and observables for event streams or HTTP data.
Signals hold a single reactive value and update UI automatically.
Observables handle streams of data over time and require subscribing.
Choose signals for simple state, observables for complex async data.
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
