Angular Signals
A signal is a wrapper around a value that tells interested code when the value changes. Signals are Angular's primary tool for reactive state: when a template reads a signal, Angular knows that this exact part of the view depends on it and updates it — and only it — when the signal changes.
Creating and reading a signal
import { signal } from '@angular/core';
const count = signal(0); // WritableSignal<number>
console.log(count()); // read: call it like a function → 0You read a signal by calling it. That call is how Angular notices the dependency.
Changing a signal
| Method | Use when | Example |
|---|---|---|
set(value) |
You know the new value | theme.set('dark') |
update(fn) |
The new value depends on the old one | count.update(c => c + 1) |
asReadonly() |
You want to expose it without allowing writes | readonly items = this._items.asReadonly() |
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-theme-toggle',
template: `
<div class="card" [class.dark]="theme() === 'dark'">
<p>Current theme: <strong>{{ theme() }}</strong></p>
<p>Toggled {{ toggles() }} times</p>
<button (click)="toggle()">Switch theme</button>
</div>
`,
styles: `
.card { padding: 16px; border-radius: 8px; background: #f7f7f7; }
.card.dark { background: #1f1f2e; color: #f1f1f1; }
`,
})
export class ThemeToggle {
theme = signal<'light' | 'dark'>('light');
toggles = signal(0);
toggle() {
this.theme.update((t) => (t === 'light' ? 'dark' : 'light'));
this.toggles.update((n) => n + 1);
}
}Build your own signal
Signals are a small idea. Here is a minimal implementation in plain TypeScript — a value, a list of listeners and an equality check that skips notifications when nothing really changed (Angular's signals behave the same way, using Object.is by default):
type Listener<T> = (value: T) => void;
function createSignal<T>(initial: T) {
let value = initial;
const listeners: Listener<T>[] = [];
const read = () => value;
return Object.assign(read, {
set(next: T) {
if (Object.is(next, value)) return; // unchanged: no notification
value = next;
listeners.forEach((l) => l(value));
},
update(fn: (current: T) => T) {
this.set(fn(value));
},
subscribe(l: Listener<T>) {
listeners.push(l);
},
});
}
const count = createSignal(0);
count.subscribe((v) => console.log('count changed to', v));
count.set(1);
count.update((c) => c + 10);
count.set(11); // same value, ignored
console.log('final:', count());count changed to 1
count changed to 11
final: 11
Note: Angular's real signals don't expose
subscribe; instead, templates,computed()andeffect()track the signals they read automatically. You'll see that in the next lesson.
Signals hold references
Because equality is checked with Object.is, mutating an object or array in place doesn't count as a change. Always produce a new value:
const seen: string[] = [];
let current: string[] = ['a'];
function setList(next: string[]) {
if (Object.is(next, current)) {
seen.push('skipped');
return;
}
current = next;
seen.push(`notified (${next.length})`);
}
current.push('b');
setList(current); // same array: skipped
setList([...current, 'c']); // new array: notified
console.log(seen);[ 'skipped', 'notified (3)' ]
Exposing read-only state
A common pattern is a private writable signal with a public read-only view. Other code can read it, but only the owning class can change it:
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-cart-badge',
template: `
<button (click)="add()">Add to cart</button>
<span class="badge">🛒 {{ count() }}</span>
`,
styles: `.badge { margin-left: 12px; font-weight: bold; }`,
})
export class CartBadge {
private readonly _count = signal(2);
readonly count = this._count.asReadonly();
add() {
this._count.update((n) => n + 1);
}
}Signal types at a glance
| Function | Type | Writable? |
|---|---|---|
signal(v) |
WritableSignal<T> |
Yes |
computed(() => …) |
Signal<T> |
No, derived |
input() / input.required() |
InputSignal<T> |
No, set by the parent |
model() |
ModelSignal<T> |
Yes, two-way with parent |
linkedSignal(() => …) |
WritableSignal<T> |
Yes, resets when its source changes |
Tip: Name signals like ordinary values (
count,user,items) — notcountSignal. The()when reading them already makes it clear.
Test yourself
How do you read the value of const total = signal(5)?
total.valuetotal.get()total()$total
Which call increments a numeric signal based on its current value?
count.set(count + 1)count.update(c => c + 1)count++count.next(1)
You push an item into the array held by a signal without calling set/update. What happens?
- Angular detects it and updates the view
- Nothing is notified, because the signal never received a new value
- The app crashes
- The signal resets