Sub topic 1
Why Signals: Fine-Grained Reactivity in Angular
Angular has always had to answer one question after every click, timer, or HTTP response: what changed, and which parts of the screen need to update? For most of its history, Angular answered by checking everything. Signals give Angular a precise answer instead. This chapter explains the problem signals solve, the reactive model behind them, and how that model differs from what you may already know from Zone.js and RxJS. Later chapters cover each API in detail.
Prerequisites
This chapter assumes you can already do the following:
- Read and write TypeScript, including generics (
signal<number>) and arrow functions. - Build Angular components with templates and data binding.
- Understand Angular dependency injection and services at a basic level.
- Know that Angular runs "change detection" to sync component state with the DOM.
- Recognize an RxJS Observable. This matters mostly for the comparison section and the interop chapter.
You do not need any prior experience with signals.
The Problem: Change Detection Without Knowing What Changed
In a classic Angular app, a library called Zone.js patches browser APIs such as addEventListener, setTimeout, and fetch. When any patched async task finishes, Zone.js tells Angular that something might have changed. Angular then walks the component tree from the root and re-evaluates every template binding, comparing each new value to the previous one. This process is called dirty checking.
Dirty checking has three weaknesses:
- It is coarse. Angular knows that something happened but not what. A click that changes one counter still causes bindings across the whole tree to be checked.
- It is indirect. Updates depend on Zone.js noticing the async task. Code that runs outside the zone, or a third-party library that schedules work in an unusual way, can leave the screen stale.
- It costs time on every event. Developers use
ChangeDetectionStrategy.OnPushto skip subtrees, but OnPush depends on input reference changes and manualmarkForCheck()calls, and these are easy to get wrong.
The root issue is that a plain class property carries no information about who reads it. When you assign this.count = 5, nothing records that a template binding depends on count. Angular has to rediscover that relationship by checking.
The Idea: Values That Know Their Readers
A signal is a wrapper around a value that records who reads it and notifies those readers when the value changes. You read a signal by calling it as a function, and that call is what allows tracking.
import { signal } from '@angular/core';
const count = signal(0); // create a signal holding 0
console.log(count()); // read: 0
count.set(5); // write: readers are notified
console.log(count()); // read: 5The function-call syntax is the main difference from a plain property. When code runs inside a reactive context (a template, a computed(), or an effect()), each signal call registers a dependency. Angular builds a graph of these dependencies at runtime, so when count changes it knows which computations and which template views read count. Only those need attention.
This is fine-grained reactivity: updates are driven by individual values and their actual readers, not by a broad "something happened somewhere" event.
The Reactive Graph: Producers and Consumers
Angular's signal implementation models everything as nodes in a graph:
- A producer is a node that holds or produces a value others can read. Writable signals and computed signals are producers.
- A consumer is a node that reads producers and must react when they change. Computed signals, effects, and component templates are consumers.
A computed signal is both: it consumes its inputs and produces a value for its own readers.
Edges are created when a consumer reads a producer during execution. Nobody declares them in advance. Each time a consumer re-runs, Angular records a fresh set of dependencies, so the graph matches what the code actually read on its most recent run.
How Updates Flow: Push Then Pull
Signals use a push-pull algorithm. The two phases solve different problems.
Push (notification). When you call set() or update() on a writable signal, it pushes a "you may be stale" notification to its consumers, and they pass it on to theirs. No values are computed in this phase. Nodes are only marked dirty.
Pull (evaluation). When someone reads a dirty computed signal, it checks whether any of its producers actually changed value. If so, it recomputes. If not, it returns its cached value. Templates and effects are scheduled to run later, and they pull fresh values when they run.
This design has three useful properties:
- Lazy. A computed value nobody reads is never recalculated, however often its inputs change.
- Memoized. Reading a computed value twice without input changes returns the cached result.
- Glitch-free. Because values are pulled after all notifications settle, a consumer never sees a mix of old and new inputs. If you update
priceandqtyback to back,totalrecomputes once, using both new values.
You can watch this happen in a plain TypeScript file. signal() and computed() do not need a component or an injection context.
import { computed, signal } from '@angular/core';
const price = signal(100);
const qty = signal(2);
let runs = 0;
const total = computed(() => {
runs++;
return price() * qty();
});
console.log(runs); // 0 -> nothing computed yet (lazy)
console.log(total()); // 200
console.log(total()); // 200 -> cached
console.log(runs); // 1
price.set(150);
qty.set(3);
console.log(runs); // 1 -> still not recomputed; only marked dirty
console.log(total()); // 450
console.log(runs); // 2 -> one recomputation for two writesDynamic Dependencies
Because dependencies are recorded per run, a branch that is not taken does not create a dependency.
import { computed, signal } from '@angular/core';
const showDetails = signal(false);
const details = signal('Full product description');
let runs = 0;
const panel = computed(() => {
runs++;
return showDetails() ? details() : 'Details hidden';
});
console.log(panel()); // 'Details hidden', runs = 1
details.set('Updated text');
console.log(panel(), runs); // 'Details hidden' 1 -> details is not a dependency yet
showDetails.set(true);
console.log(panel(), runs); // 'Updated text' 2
details.set('Newer text');
console.log(panel(), runs); // 'Newer text' 3 -> now details is trackedThis matters when you debug. If a computed value does not update, check whether the signal was actually read on the last run. A read placed behind an early return, or inside a callback that runs later (such as a setTimeout), is not tracked.
Equality Stops Useless Updates
A writable signal compares its new value with the old one before it notifies anyone. By default it uses Object.is. Setting count.set(5) when the value is already 5 does nothing. The same check applies to computed signals: if a computed recomputes and produces an equal value, its own consumers are not marked as changed.
This has a direct consequence for objects and arrays. Mutating an array in place and then setting the same reference does not count as a change, because Object.is(arr, arr) is true. Signal-based code therefore favors creating new objects and arrays on update. The chapter on equality and untracked() shows how to supply a custom equality function.
How Signals Compare to What You Know
| Aspect | Plain properties + Zone.js | Signals | RxJS Observables |
|---|---|---|---|
| Holds a current value | Yes | Yes, always | Not necessarily (`BehaviorSubject` does) |
| Knows its readers | No | Yes, via the dependency graph | Only explicit subscribers |
| Reading | Property access | Function call `count()` | `subscribe()` or `async` pipe |
| Derived values | Getters, re-run on every check | `computed()`, lazy and memoized | Operators such as `map`, `combineLatest` |
| Timing | Synchronous state, checked later | Synchronous reads, scheduled renders | Can model time: debounce, retry, cancellation |
| Cleanup | Not needed | Automatic for templates and computed | Manual unsubscribe or `takeUntil` |
| Best at | Legacy code | Synchronous UI state | Event streams and async workflows |
Signals do not replace RxJS. Signals model state, a value that exists right now. Observables model events over time. A search box is a good example: the query text is state, and the debounced stream of HTTP requests is an event pipeline. The RxJS interop chapter connects the two with toSignal() and toObservable().
What This Means for Change Detection
When a template reads a signal, the template's view becomes a consumer in the reactive graph. A change to that signal marks that specific view for refresh and schedules change detection. Angular no longer has to guess. This is what makes signals work cleanly with OnPush components and allows apps to run without Zone.js entirely. Recent Angular versions create new projects in zoneless mode by default. The change detection chapter covers the details.
Common Misconceptions
- "Signals make every app faster." Signals remove wasted checking. If an app was already fast, the main gain is predictability, not speed.
- "Calling a signal is expensive, so cache it in a variable." A read is cheap. Caching the value in a local variable inside a template expression or computed is fine, but caching it in a class field breaks reactivity.
- "A computed runs whenever its inputs change." A computed runs when it is read after its inputs change. Unread computed signals do no work.
- "Mutating an object inside a signal updates the UI." In-place mutation keeps the same reference, so the default equality check sees no change.
Exercise
A computed signal fullName reads first() and last(). You call first.set('Grace') and then last.set('Hopper'), and nothing reads fullName afterward. How many times does fullName recompute, and why?
Show answer
Zero times. Both writes only push dirty notifications. A computed signal recalculates lazily when it is read, and nothing reads it. If it were read once after both writes, it would recompute exactly once, using both new values.
Exercise
An array signal holds ['a', 'b']. A developer runs const arr = items(); arr.push('c'); items.set(arr); and the template does not update. Explain the cause using the reactive model.
Show answer
set() compares the new value with the old one using Object.is. arr is the same reference the signal already holds, so the comparison is true and no consumers are notified. Creating a new array, for example items.set([...items(), 'c']), produces a different reference and triggers the update.
Exercise
Inside a computed, the code is if (!enabled()) return 0; return price() * qty();. While enabled() is false, price changes several times. Does the computed recompute on the next read? What changes once enabled becomes true?
Show answer
No. On its last run the computed returned early, so it depends only on enabled. Changes to price and qty do not mark it dirty. Once enabled becomes true, the next read recomputes, reads price and qty, and records them as dependencies, so later changes to either one cause recomputation.