Signals and computed

A signal holds a value that changes, and tells Angular when it does, so the page always shows the latest. Read it by calling it, change it with set or update, and derive values from signals with computed, which recalculates by itself.

Apprentice · Angular · Updated

After this lesson you can

  • Hold a component's changing data in signals, and change them with set and update
  • Derive values from other signals with computed

Key takeaways

  • Put data that changes in a signal: Angular isn't told when a plain property changes later, after a timer, so the page can show an old value.
  • Read a signal by calling it, count(); give it a new value with count.set(5), or work it out from the old one with count.update(c => c + 1).
  • To change a list in a signal, give it a new array, such as [...list, newItem]. Changing the old array with push doesn't tell the signal anything.
  • computed makes a read-only signal that recalculates by itself when the signals it reads change, as total follows items.
  • Keep the real data in signals and everything worked out from it in computeds, so nothing drifts out of step.

A component's state is the data that changes while it's on the page: what's in the cart, which tab is open, what the user typed. In Angular, state goes in signals.

Why not a plain property?

Angular has to know when to update the page. Here's a component with one plain property and one signal, both changed half a second after a click:

import { Component, signal } from '@angular/core';

@Component({
  imports: [],
  selector: 'app-later',
  template: `
    <p>Plain: {{ plainCount }}</p>
    <p>Signal: {{ count() }}</p>
    <button (click)="addLater()">Add 10, later</button>
  `,
})
export class Later {
  protected plainCount = 0;
  protected readonly count = signal(0);

  protected addLater() {
    setTimeout(() => this.plainCount += 10, 500);
    setTimeout(() => this.count.update(c => c + 10), 1000);
  }
}

Click, and after half a second the plain property is 10, but the page still says Plain: 0: nothing told Angular that it changed. After a whole second the signal changes, Angular updates the component, and now both numbers show 10.

Changes that happen later, after a timer, or when data arrives from an API, are exactly the ones that matter. A signal tells Angular "I changed", every time, so the page is never out of date. That's why all state goes in signals.

Reading and changing a signal

const count = signal(0);

count();                    // read it: 0
count.set(5);               // give it a new value
count.update(c => c + 1);   // work out the new value from the old: 6
To Write
Create it, with a first value signal(0), or signal<Category[]>([]) with a type
Read it, in the class or the template count()
Give it a new value count.set(5)
Change it based on its old value count.update(c => c + 1)

For a list, update returns a new array: items.update(list => [...list, newItem]) to add, items.update(list => list.filter(i => i !== item)) to remove. A signal notices a new value; changing the old array in place (push) wouldn't tell it anything.

computed: values worked out from signals

Some values are worked out from others: a cart's total from its items. computed makes a signal that recalculates by itself whenever the signals it reads change:

import { Component, computed, signal } from '@angular/core';
import { CurrencyPipe } from '@angular/common';

interface CartItem {
  name: string;
  price: number;
}

@Component({
  imports: [CurrencyPipe],
  selector: 'app-cart',
  template: `
    <button class="btn btn-outline-primary me-2" (click)="add('Chai', 18)">Add Chai</button>
    <button class="btn btn-outline-primary" (click)="add('Konbu', 6)">Add Konbu</button>
    <p class="mt-3">Items: {{ itemCount() }}, total {{ total() | currency }}</p>
  `,
})
export class Cart {
  protected readonly items = signal<CartItem[]>([]);
  protected readonly itemCount = computed(() => this.items().length);
  protected readonly total = computed(() => this.items().reduce((sum, item) => sum + item.price, 0));

  protected add(name: string, price: number) {
    this.items.update(items => [...items, { name, price }]);
  }
}

Click Add Chai, then Add Konbu: the page shows Items: 1, total $18.00, then Items: 2, total $24.00. You never update itemCount or total yourself: they read items, so they follow it.

  • A computed is read-only: it has no set.
  • It's only recalculated when needed, when something reads it after one of its signals changed.
  • Keep the real data in plain signals, and everything worked out from it in computeds. Then nothing can drift out of step.

Check yourself

2 questions to try on the lesson page.

So what

Put every value that changes in a signal, and every value worked out from them in a computed. Angular then always knows what changed, and the page shows the truth, however and whenever the data changes.

Key terms

Cheat sheets