What Is a Signal?
A signal is a box that holds a value and tells Angular when the value changes. Angular then updates only the parts of the screen that use that value.
Simple example: A spreadsheet. If cell C1 is =A1+B1, changing A1 updates C1 automatically. Signals work like this for your component data.
Writable Signals
typescript
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-counter',
template: `
<p>Count: {{ count() }}</p>
<button (click)="increment()">+1</button>
<button (click)="reset()">Reset</button>
`,
})
export class Counter {
count = signal(0);
increment() { this.count.update(c => c + 1); }
reset() { this.count.set(0); }
}
| Action | Code |
| Create | const count = signal(0) |
| Read | count() (call it like a function) |
| Replace | count.set(5) |
| Change based on old value | count.update(c => c + 1) |
Do not forget the brackets: In the template write {{ count() }}, not {{ count }}. The brackets read the current value.
computed: Derived Values
typescript
export class Cart {
items = signal([
{ name: 'Book', price: 12, qty: 2 },
{ name: 'Pen', price: 2, qty: 5 },
]);
total = computed(() =>
this.items().reduce((sum, i) => sum + i.price * i.qty, 0));
count = computed(() => this.items().length);
isEmpty = computed(() => this.count() === 0);
}
A computed signal is read-only, recalculates only when the signals it uses change and caches its result.
Update Arrays and Objects the Immutable Way
typescript
addItem(item: Item) {
this.items.update(list => [...list, item]); // new array
}
remove(name: string) {
this.items.update(list => list.filter(i => i.name !== name));
}
changeQty(name: string, qty: number) {
this.items.update(list => list.map(i => i.name === name ? { ...i, qty } : i));
}
Never mutate: this.items().push(x) changes the array without telling Angular. Always create a new array or object with spread, map or filter.
effect: Run Code When Signals Change
typescript
constructor() {
effect(() => {
console.log('Cart total is now', this.total());
localStorage.setItem('cart', JSON.stringify(this.items()));
});
}
Use effect for side effects: logging, saving to local storage, drawing on a canvas. Do not use it to set other signals. Use computed for that.
linkedSignal: Resettable Derived State
typescript
options = signal(['S', 'M', 'L']);
selected = linkedSignal(() => this.options()[0]); // resets to first option when options change
choose(size: string) { this.selected.set(size); } // but you can still set it
Signals vs Plain Properties vs Observables
| Signal | Plain property | Observable (RxJS) |
| Current value | Always (s()) | Always | No, only when it emits |
| Best for | Component and UI state | Constants | Streams over time: HTTP, events, websockets |
| Updates view | Automatically and precisely | Needs change detection | With async pipe or toSignal |
typescript
// read without tracking inside an effect
import { untracked } from '@angular/core';
effect(() => { const a = this.a(); untracked(() => console.log(this.b())); });
Conclusion
Create state with signal, derive values with computed, run side effects with effect and always update with set or update using new objects and arrays.
Quick Summary: signal() holds state, computed() derives, effect() reacts; read with ().