Signals, computed values, and effects
The signal(), computed(), and effect() primitives Angular's reactivity model is now built around.
Signals, computed values, and effects
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A signal is a value plus notification — reading it with `count()` both returns the current value and tells Angular that whoever's reading it cares when it changes.
- `computed()` derives a new signal from others and recalculates automatically, lazily, and only when something actually reads it after a dependency changed.
- `effect()` runs side effects in response to signal changes, and must be used deliberately — most "I need to react to a change" problems are better solved with `computed()` than with an effect.
A plain value can’t tell you when it changes
class Counter {
count = 0; // a plain property
}
Nothing about count here can notify anything else when it changes. If a
template displays count, and count changes, something external has to
already know to re-check and re-render — which is exactly the job Angular’s
change-detection system has historically had to do by scanning the whole
component tree, because plain properties give it no other way to find out what
changed.
A signal is a value with that notification built in.
signal(): a value, plus the ability to be watched
count = signal(0);
increment() {
this.count.update((n) => n + 1);
// or: this.count.set(this.count() + 1);
}
count here isn’t a plain number — it’s a function-shaped wrapper around one.
Reading it is this.count(), a function call, not a property read. That call
does two things at once: it returns the current value, and it registers “this
read happened inside some reactive context that cares about this signal” — so
that whatever was reading it (a template, a computed(), an effect()) can be
notified the next time the signal changes. .set() replaces the value outright;
.update() derives the new value from the current one, which matters the moment
the update itself depends on the current state, like the increment above.
computed(): derived values that recompute themselves
price = signal(29.99);
quantity = signal(3);
total = computed(() => this.price() * this.quantity());
total is itself a signal, but you never set it directly — its value is
entirely determined by reading price and quantity inside the function you
gave it. Change either one, and total automatically reflects the new result the
next time something reads it. Two properties are worth being precise about:
lazy — the function inside only runs when total() is actually read, not
the instant a dependency changes — and memoized — once computed, the result
is cached until a dependency actually changes again, so reading total()
repeatedly without any dependency changing doesn’t re-run the calculation each
time.
effect(): reacting to changes, used sparingly
effect(() => {
console.log(`Total is now ${this.total()}`);
});
An effect runs a side effect — logging, writing to local storage, calling an
imperative API — automatically whenever any signal it reads changes. It’s the
one signal primitive that exists for doing something rather than holding a
value, and that distinction is worth sitting with: if what you actually want is
a derived value, computed() is almost always the right tool, not an effect
that manually sets another signal in response to a change. Reach for effect()
specifically when the reaction is something outside Angular’s own reactivity
system entirely — synchronizing with a non-Angular API, for instance — not as a
general-purpose “run this when that changes” hammer.
Why this is a different foundation, not an added feature
Signals aren’t layered on top of Angular’s existing change-detection system as an
optional extra — they’re the foundation the next few lessons build on directly:
how change detection itself decides what to check, how OnPush components
interact with signal reads, and eventually how an app can run with no zone-based
change detection at all. This lesson’s video builds signal() and computed()
from first principles with a running example; the two linked videos go further —
one a deep, longer treatment of the whole signals system, the other focused
specifically on effect() and its cleanup behavior.
Check yourself
Why does `count()` need parentheses, when a plain class property like `this.total` doesn't?
`count` is a signal — a function-shaped object, not a plain value — so `count()` is calling it to read the current value, which is also how Angular tracks that whatever's doing the reading depends on this signal. A plain property is just a value sitting in memory with no way to notice who read it or when.
A computed signal `fullName = computed(() => `${firstName()} ${lastName()}`)` is never read anywhere in a template or an effect. Does it ever actually run?
No — computed signals are lazy. The function inside only runs when something actually reads `fullName()`, and even then it's cached until one of its dependencies (`firstName` or `lastName`) changes. An unread computed signal just sits there, doing no work.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.