Signals and change detection inside
Signals form a graph. Writing a signal only marks its readers as dirty (push), and a computed value is calculated again only when someone reads it (pull). Angular tracks which signals were read during the last run, so dependencies can change. A template is a reader too, so in a zoneless app a signal change refreshes only the components that read it. These rules explain most strange bugs with computed and effect.
Author: bezzad
Contents
The problem: “my computed does not update”
The Angular fundamentals lesson shows how to use signal, computed and effect. In a senior interview the question is different: why does it behave like this? Our shop team has three bugs:
- A price filter does not update when the user moves the slider.
- An effect runs once, then never again, although the data changes.
- A log in an effect shows only the last value, not every change.
To explain them, you need to know how signals work inside.
A graph of producers and consumers
- A producer holds a value: signal, and also computed.
- A consumer reads producers: computed, effect, and every component template.
- While a consumer runs, Angular records every signal it reads. These are its dependencies for the next time.
Push, then pull
readonly items = signal<CartItem[]>([]);
readonly total = computed(() =>
this.items().reduce((sum, i) => sum + i.price * i.quantity, 0),
);
readonly freeShipping = computed(() => this.total() >= 50);
- Push: items.set(…) increases the version of items and marks total and freeShipping as maybe dirty. No function runs.
- Pull: when somebody reads freeShipping, it asks total. total sees that items has a new version and runs its function again.
- Lazy: a computed that nobody reads never runs. You can define many without a cost.
- Memoized: reading total ten times in a row runs the function once.
- Equality stops the wave: if total is calculated again but the result is the same (Object.is by default), freeShipping does not run again. You can pass your own equal function.
- Glitch-free: a reader never sees a mix of new items and an old total.
Dynamic dependencies
readonly label = computed(() =>
this.showDiscount() ? this.salePrice() : this.price(),
);
- When showDiscount is false, this run reads only showDiscount and price. salePrice is not a dependency now. Changing it does nothing, and that is correct.
- This explains bug 2: an effect that returns early before reading a signal does not depend on that signal.
- Reads after an await are not tracked. Tracking only works while the function runs synchronously. Read all signals first, then start async work.
- untracked(() => …) reads a signal without making it a dependency. Use it, for example, for a logger or for the current user inside an effect that should run only when the cart changes.
Only signals are tracked
This explains bug 1:
maxPrice = 100; // plain field
readonly visible = computed(() =>
this.products().filter((p) => p.price <= this.maxPrice),
);
A plain field is not a producer. The computed has products as its only dependency, so changing maxPrice never makes it dirty. It keeps the old, memoized list. Make maxPrice a signal (or an input), and it works.
Effects run later, and only once per change set
- An effect does not run at the moment you set a signal. Angular schedules it and runs it later, during change detection.
- If you set a signal three times in one click handler, the effect runs once, with the last value. This is bug 3. If you need every value, it is an event, not state. Use an output, a Subject or call a function directly.
- Writing signals inside computed throws an error. A computed must be pure.
- Writing signals inside an effect is allowed, but it is usually a sign that you need computed or linkedSignal.
- Effects created in a component are destroyed with it. Effects in root services live for the whole app.
Templates are consumers: targeted refresh
- A component’s template is a reactive consumer. When a signal it read changes, Angular marks that view for refresh and its parents as “has a child to refresh”.
- With OnPush components, change detection walks down to that view and checks only it. The parents are passed, not checked again.
- In a zoneless app, a signal change is also what schedules change detection. Without zone.js, nothing else tells Angular to look.
- A plain field changed in setTimeout does not schedule anything in zoneless mode, so the page stays old. In the default mode with zone.js, it works by luck, because zone.js runs a full check after every timer.
Tools around signals
- linkedSignal: a writable signal that resets when its source changes. For example the selected size resets when the product changes, but the user can still pick a size.
- resource and httpResource: load async data from signals, with value, error and isLoading signals. They cancel the old request when the input changes. Check their status in your version; parts are still marked experimental.
- toSignal and toObservable: bridges to RxJS. toSignal subscribes at once and needs an initial value or requireSync.
Common mistakes
| Mistake | Result | Better way |
|---|---|---|
| A plain field inside computed | The computed never updates for it. | Make it a signal or an input. |
| Reading a signal after await | Not a dependency. | Read signals first, then await. |
| Effect to copy one signal to another | Late values, extra runs. | computed, or linkedSignal. |
| Expecting an effect per set call | Only the last value is seen. | Events are not state: call a function or emit. |
| Setting a signal in computed | Runtime error. | Keep computed pure. |
| Plain fields in a zoneless app | The page does not refresh. | Keep template state in signals. |
Summary in 6 lines
- Signals are a graph of producers and consumers. Templates are consumers too.
- Writing pushes “dirty” down. Reading pulls new values up. Computed is lazy and memoized.
- Dependencies are the signals read in the last synchronous run. Plain fields and reads after await are not tracked.
- Same value by equality: the wave stops there.
- Effects run later, once per change set, during change detection.
- With OnPush or zoneless, a signal change refreshes only the views that read it.