Sub topic 1
Why Signals: Fine-Grained Reactivity in Angular
Angular has always kept the screen in sync with your data. Before signals, it did this by checking. After any event, timer, or HTTP response, Angular walked the component tree and compared template bindings to see what changed. Signals give Angular a way to know which values changed and which parts of the UI read them. This chapter explains that model. Later chapters cover each API in detail.
Prerequisites
This chapter assumes you can:
- Write TypeScript with generics and arrow functions.
- Build Angular components with templates and data binding.
- Describe, in broad terms, what Angular change detection does.
You do not need any prior experience with signals.
The Problem with Checking Everything
In the classic model, Zone.js patches browser APIs such as setTimeout, addEventListener, and fetch. When one of these callbacks finishes, Zone.js tells Angular that something might have changed. Angular cannot know what changed, so it runs change detection across the component tree.
This works, but it has costs:
- Work grows with the size of the tree, not with the size of the change.
- Angular has no record of which data a binding depends on, so it re-evaluates bindings to find out.
- Developers use
OnPushand manualmarkForCheck()calls to limit the work, and these are easy to get wrong.
What a Signal Is
A signal is a wrapper around a value that notifies interested consumers when that value changes. You read a signal by calling it as a function. That call does two things. It returns the current value, and it records that the code currently running depends on this signal.
The code that reads signals is called a consumer. Consumers include a computed() value, an effect(), and a component template. Signals that hold values are called producers. Together they form a dependency graph that Angular builds automatically while your code runs.
import { signal, computed } from '@angular/core';
const price = signal(20);
const quantity = signal(3);
// Reading price() and quantity() registers them as dependencies.
const total = computed(() => price() * quantity());
console.log(total()); // 60
quantity.set(5); // total is marked stale, not recomputed yet
console.log(total()); // 100, recomputed on this readYou never declared that total depends on price and quantity. Angular recorded the dependency when the computation ran.
Push, Then Pull
Signals use a push-pull model:
- Push: when you write to a signal, Angular marks its consumers as possibly stale. No values are recomputed yet.
- Pull: when something reads a stale consumer, it recomputes, and only if one of its dependencies actually changed.
Because templates are consumers too, Angular knows exactly which components read a changed signal. It can refresh those components and skip the rest. This is what fine-grained reactivity means, and it is also why signals make Zone.js optional.
Common Misunderstandings
| Misunderstanding | What actually happens |
|---|---|
| A signal is just an Observable | A signal always has a current value and is read synchronously. It has no subscribe step. |
| Reading `count` in a template works | You must call `count()`. Without the call there is no value and no tracked dependency. |
| Mutating an array inside a signal triggers updates | By default, signals compare values with `Object.is`. Pushing into the same array keeps the same reference, so consumers are not notified. |
| `computed()` reruns on every read | It caches its value and recomputes only after a dependency changes. |
Where This Tutorial Goes Next
The next chapters build on this model. First come writable signals, computed(), and effect(). After those, the tutorial shows how templates, OnPush, and zoneless apps use the graph. Then it moves to signal-based component APIs and shared state.
Exercise
In the code example above, you call quantity.set(5) and never read total() again. How many times does the computed function run after the set call, and why?
Show answer
Zero times. The write only marks total as stale (the push step). The computation runs again only when something reads total() (the pull step), so with no read it never reruns.
Exercise
A component stores items = signal<string[]>([]) and runs this.items().push('new'). The template shows items().length. Why does the screen not update?
Show answer
push mutates the existing array, so the signal still holds the same reference. Object.is sees no change and no consumers are notified. Use this.items.update(list => [...list, 'new']) to set a new array.