Sub topic 1
Signals in two minutes
Prerequisites
This chapter assumes you know the following:
- TypeScript, including
async/awaitand Promises. - Angular components and templates.
- The basics of
HttpClientand RxJS Observables.
You do not need to have used signals in depth. This chapter recaps the parts the resource API relies on.
Why signals come first
A resource is built on signals. Its inputs are signals, and its outputs (value, status, error) are signals too. If you can read, write, and derive signals, the rest of the tutorial follows the same model.
The three building blocks
A signal is a wrapper around a value that tells Angular when the value changes. You read it by calling it like a function.
import { computed, effect, signal } from '@angular/core';
const count = signal(0);
const double = computed(() => count() * 2);
effect(() => {
console.log(`count=${count()}, double=${double()}`);
});
count.set(2);
count.update((n) => n + 1);Each building block has a distinct job:
| Function | Purpose | Writable |
|---|---|---|
| `signal(initial)` | Holds a value you change with `set()` or `update()` | Yes |
| `computed(fn)` | Derives a value from other signals and caches the result | No |
| `effect(fn)` | Runs a side effect when the signals it reads change | Not applicable |
set(value) replaces the value. update(fn) computes the new value from the old one. Both notify anything that depends on the signal.
Dependency tracking
Angular records which signals a function reads while it runs. When one of them changes, Angular knows what to re-run. You never list dependencies by hand.
This is the mechanism a resource uses. Its params function reads signals. When any of them changes, the resource notices and runs its loader again.
import { computed, signal } from '@angular/core';
const userId = signal(1);
const params = computed(() => ({ id: userId() }));
console.log(params()); // { id: 1 }
userId.set(2);
console.log(params()); // { id: 2 }The params function you will write in later chapters behaves like this computed. It returns a value built from signals, and a new value means new work.
Two details that matter later
Signals have a few rules that cause common mistakes with resources.
- Reads must happen inside the tracking function. A signal read outside a
computed,effect, or template is a one-time snapshot. Later changes do not reach it. - Equal values do not notify. Setting a signal to a value that is equal (by
Object.is) to the current one triggers nothing. Setting a new object each time does trigger updates, even if its contents match.
Check your understanding
Exercise
A component has const page = signal(1) and const label = computed(() => 'Page ' + page()). You call page.set(1). Does anything that reads label() re-run, and why?
Show answer
No. The new value equals the current value under Object.is, so the signal does not notify its dependents. If you set page to 2, label would be recomputed and its readers would re-run.
Exercise
Why is const snapshot = count(); at the top level of a file not reactive?
Show answer
The read happens outside any tracking context such as computed, effect, or a template. Angular records no dependency, so snapshot keeps the value it had at that moment.