Sub topic 1
Signals and observables: different tools for different jobs
Angular now has two reactive tools. Signals hold state. RxJS observables describe events over time. Later chapters convert between them with toSignal and toObservable. This chapter explains what each tool is for, so you can tell when a conversion is worth making.
Prerequisites
This chapter assumes you know:
- How to write an Angular component and inject a service.
- How to create and read signals with
signal()andcomputed(). - Basic RxJS: what an
Observableis, howsubscribeworks, and operators such asmapandswitchMap. - Enough TypeScript to read generics like
Signal<number>.
What a signal is
A signal is a wrapper around a value that Angular can track. You read it by calling it, and you change it with set or update.
import { signal, computed } from '@angular/core';
const quantity = signal(2);
const price = signal(10);
const total = computed(() => quantity() * price());
console.log(total()); // 20
quantity.set(3);
console.log(total()); // 30Three properties matter here:
- A signal always has a current value. You can read it at any moment, and it is never "not yet available".
- Reading is synchronous.
total()returns a number, not a promise or a stream. - Angular records which signals a template or
computedreads. When one of them changes, Angular updates only the parts that depend on it.
Signals describe what the state is now. They don't keep a history. If you set a signal three times, a reader afterwards sees only the last value.
What an observable is
An observable is a description of values that may arrive over time, now or later or never. Nothing happens until someone subscribes.
import { interval, map, take } from 'rxjs';
const doubled$ = interval(1000).pipe(
take(3),
map((n) => n * 2),
);
doubled$.subscribe({
next: (v) => console.log(v), // 0, then 2, then 4
complete: () => console.log('done'),
});Observables differ from signals in these ways:
- They may have no current value. Before the first emission, there is nothing to read.
- They can complete or fail with an error. A signal does neither.
- Every emission is delivered to subscribers, including values that arrive close together.
- Operators such as
debounceTime,switchMap,retryandcombineLatestlet you control timing, cancellation and merging of several sources.
Comparing the two
| Question | Signal | Observable |
|---|---|---|
| Has a value right now? | Always | Only after an emission |
| How do you read it? | Call it: `count()` | Subscribe |
| Can it end? | No | Yes, by completing or erroring |
| Are intermediate values kept? | No, only the latest | Yes, every emission is delivered |
| Best at | UI state and derived values | Events, async work, time-based logic |
| Cleanup | Automatic, nothing to unsubscribe | You must unsubscribe, or use a helper |
Choosing between them
Ask what the thing represents.
- If it is a value that the template shows or other values derive from, use a signal. Examples are a selected tab, a form field's text, or a total price.
- If it is something that happens, use an observable. Examples are a click, a WebSocket message, or an HTTP request.
- If the logic needs time-based operators, use RxJS for that part. Examples are debouncing, cancelling a stale request, retrying, or combining streams.
Many features need both. Take a search box. The text the user typed is state, so a signal fits. Waiting 300 ms and then cancelling the previous request is stream logic, so RxJS fits. The results shown on screen are state again, so a signal fits again.
The arrows between the boxes are the conversions. toObservable carries state into the stream. toSignal carries the stream's output back into state. Later chapters build this pipeline step by step.
Why not use only one?
Using only observables works, but templates then need the async pipe. You must also handle the missing first value yourself. Using only signals works for state, but signals have no built-in way to debounce, cancel in-flight work, or retry.
The two tools solve different problems. Treat conversion as a bridge at the boundary between them, not as a goal.
What comes next
The conversion functions live in @angular/core/rxjs-interop. Both need an injection context, a place where Angular's inject() works, such as a constructor or a field initializer. The next chapter explains why. After that, toSignal comes first, because most apps start with observables from HttpClient or the router and want to read them in templates.
Exercise
For each item, say whether a signal or an observable is the better fit: (a) the currently selected tab, (b) clicks on a button that must be throttled to one per second, (c) the total price derived from a quantity and a unit price.
Show answer
(a) A signal, because it is state with a current value that the template reads. (b) An observable, because clicks are events over time and throttling is a time-based operator such as throttleTime. (c) A signal, specifically a computed signal, because it is a derived value that updates automatically when quantity or price changes.