Sub topic 1
Recap: writable signals
Prerequisites
This chapter assumes you know basic TypeScript, including generics and arrow functions. It also assumes you can build an Angular component with a template and inject a service. You should have a high-level idea of change detection, meaning how Angular decides when to refresh the screen. Nothing here is new material. It sets up the vocabulary the rest of the tutorial uses for computed() and effect().
What a writable signal is
A signal is a wrapper around a value that tells Angular when the value changes. A writable signal is one you can change directly. You create it with signal(), passing an initial value.
To read a signal, you call it like a function. That call is how Angular learns who depends on the value. If a template or a computed value reads a signal, Angular records that read and updates the reader when the signal changes.
import { signal } from '@angular/core';
const count = signal(0);
console.log(count()); // 0TypeScript infers the type from the initial value. Here count is a WritableSignal<number>. When the initial value does not show the full type, give it explicitly:
import { signal } from '@angular/core';
const selectedId = signal<number | null>(null);
selectedId.set(42);Changing a value: set and update
A writable signal has two main methods for changing its value.
set(value)replaces the value with the one you pass.update(fn)receives the current value and returns the new one. Use it when the new value depends on the old one.
import { signal } from '@angular/core';
const count = signal(0);
count.set(10);
count.update((c) => c + 1);
console.log(count()); // 11Both methods notify the signal's readers. A reader is anything that read the signal earlier, such as a template or another signal built from it.
Equality check on write
A signal does not notify readers if you set a value equal to the current one. By default, equality uses Object.is. For numbers and strings this works as you would expect. For objects and arrays it compares references, not contents.
That detail causes the most common mistake with writable signals: changing an object in place.
import { signal } from '@angular/core';
const user = signal({ name: 'Ada', age: 36 });
// Wrong: the object is the same reference, so no notification is guaranteed
user().age = 37;
// Right: create a new object
user.update((u) => ({ ...u, age: 37 }));Treat the value inside a signal as immutable. Replace it instead of editing it.
Signals in a component
A component usually keeps its state in signals and reads them in the template by calling them.
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-counter',
template: `
<button (click)="increment()">Clicked {{ count() }} times</button>
<button (click)="reset()">Reset</button>
`,
})
export class CounterComponent {
count = signal(0);
increment() {
this.count.update((c) => c + 1);
}
reset() {
this.count.set(0);
}
}The template calls count(), so Angular tracks that read. When an event handler changes the signal, Angular knows exactly which view depends on it and refreshes it.
Exposing a read-only view
Sometimes a service should let others read a signal but not change it. Call asReadonly() to get a version without set() and update().
import { Injectable, signal } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class CartService {
private readonly _itemCount = signal(0);
readonly itemCount = this._itemCount.asReadonly();
add() {
this._itemCount.update((n) => n + 1);
}
}Consumers can call itemCount() but cannot write to it. Only CartService controls changes. This pattern also previews computed(), which is always read-only.
How the pieces connect
The diagram shows the flow you will build on in later chapters. A write marks readers as out of date, and the readers refresh when they are next needed.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Forgetting the parentheses in a template, as in `{{ count }}` | Shows the signal function, not its value | Write `{{ count() }}` |
| Mutating an object or array in place | Readers may not update | Use `set()` or `update()` with a new value |
| Calling `set()` with the same primitive | Nothing happens, by design | No fix needed |
| Exposing the writable signal from a service | Any consumer can change it | Expose `asReadonly()` |
Exercise
A component holds items = signal<string[]>([]). You write this.items().push('apple') and the template does not update. Why, and how do you add the item correctly?
Show answer
push changes the existing array, so the signal's value keeps the same reference and the equality check sees no change. Create a new array instead: this.items.update((list) => [...list, 'apple']).
Exercise
What is the difference between set() and update(), and when should you prefer update()?
Show answer
set(value) replaces the value directly. update(fn) passes the current value to a function and stores the result. Prefer update() when the new value is based on the old one, such as incrementing a counter or appending to an array.