TExcelTAD
Home › Angular › Angular Signals

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

ts
import { signal } from '@angular/core';

const count = signal(0);   // WritableSignal<number>
console.log(count());      // read: call it like a function → 0

You 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()
angular · try it yourself
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):

ts · try it yourself
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() and effect() 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:

ts · try it yourself
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:

angular · try it yourself
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) — not countSignal. The () when reading them already makes it clear.

Test yourself

How do you read the value of const total = signal(5)?

  1. total.value
  2. total.get()
  3. total()
  4. $total

Which call increments a numeric signal based on its current value?

  1. count.set(count + 1)
  2. count.update(c => c + 1)
  3. count++
  4. count.next(1)

You push an item into the array held by a signal without calling set/update. What happens?

  1. Angular detects it and updates the view
  2. Nothing is notified, because the signal never received a new value
  3. The app crashes
  4. The signal resets