Video lecture
Watch on YouTube(opens in a new tab)Sub topic 11
Reactive State with Signals
This chapter assumes you have already built a standalone component and used interpolation and event binding in its template. You should also be comfortable with TypeScript generics and arrow functions.
What a Signal Is
A signal is a wrapper around a value that tells Angular when the value changes. You read it by calling it like a function, and you change it through its own methods. Angular records which templates and computed values read a signal. When the signal changes, Angular updates only those consumers.
Angular creates signals with three functions:
| Function | Purpose | Writable? |
|---|---|---|
| `signal(initial)` | Holds a piece of state | Yes, with `set()` and `update()` |
| `computed(fn)` | Derives a value from other signals | No |
| `effect(fn)` | Runs side effects when signals it reads change | Not a value |
A Counter with Derived State
import { Component, computed, effect, signal } from '@angular/core';
@Component({
selector: 'app-counter',
template: `
<button (click)="increment()">Count: {{ count() }}</button>
<button (click)="reset()">Reset</button>
<p>Double: {{ double() }}</p>
`,
})
export class Counter {
count = signal(0);
double = computed(() => this.count() * 2);
constructor() {
effect(() => console.log('count is now', this.count()));
}
increment() {
this.count.update((c) => c + 1);
}
reset() {
this.count.set(0);
}
}Each piece does the following:
countis aWritableSignal<number>. The template reads it withcount().update()computes the new value from the current one.set()replaces the value outright.doubleis computed lazily and memoized. It recalculates only whencountchanges, and only when something reads it.effect()must run in an injection context, such as the constructor or a field initializer. It is meant for side effects like logging or syncing tolocalStorage. Do not use it to copy one signal's value into another; usecomputedfor that.
Common Errors
Forgetting the parentheses
Writing {{ count }} in a template prints the signal function itself, not its value. Always read a signal with count().
Mutating objects and arrays in place
Signals compare values with Object.is by default. If you push into an array and pass the same reference back, Angular sees no change:
import { signal } from '@angular/core';
const items = signal<string[]>([]);
// Wrong: same array reference, consumers are not notified
items.update((list) => {
list.push('milk');
return list;
});
// Right: return a new array
items.update((list) => [...list, 'milk']);Trying to set a computed signal
A computed signal is read-only, and it has no set() method. To change a derived value, change the signals it depends on.
Why Signals Matter
Before signals, Angular relied on Zone.js to guess when to check the whole component tree. Signals tell Angular exactly which values changed and where they are used. This allows finer-grained updates and makes zoneless change detection possible. Signal-based inputs, which the next chapter covers, are built on the same primitive.
Exercise
A component has price = signal(10) and quantity = signal(3). Write a total value that always equals price times quantity, plus a method addOne() that increases quantity by one.
Show answer
Declare total = computed(() => this.price() * this.quantity()); and addOne() { this.quantity.update((q) => q + 1); }. Because total is computed, it recalculates whenever price or quantity changes and cannot be set directly.
Exercise
Why does tags.update((t) => { t.push('new'); return t; }) fail to refresh the template, and how do you fix it?
Show answer
The callback returns the same array reference, so the Object.is equality check reports no change and consumers are not notified. Return a new array instead: tags.update((t) => [...t, 'new']).