← Back to Angular, page 1

angular-0005 · Easy fresher

What are signals in Angular, and how do signal(), computed(), set() and update() work?

A signal is a wrapper around a value that tells Angular when the value changes. signal() creates a writable one, computed() derives a read-only value that updates automatically, and set() and update() change the stored value. You read a signal by calling it like a function.

Full answer

A signal holds a value and keeps a list of who is reading it. When the value changes, the readers are told. Angular uses this to update only the parts of the screen that depend on that value.

  • const marks = signal(80) creates a writable signal. Read it with marks().
  • marks.set(90) replaces the value.
  • marks.update(m => m + 5) computes the new value from the old one.
  • const grade = computed(() => marks() >= 75 ? 'A' : 'B') creates a read-only signal that is calculated from other signals.

computed is lazy and cached. It does not run until someone reads it, and it does not run again until a signal it used has changed. If nothing changed, reading it returns the stored result. This is like a canteen bill that is only re-added when you add a new item.

Signals must be read by calling them. In a template write {{ marks() }}, not {{ marks }}.

Updates must not mutate objects in place. If a signal holds an array, list.update(l => [...l, x]) notifies readers, but list().push(x) does not, because the signal sees the same array reference.

Signals are stable in Angular 20 for signal, computed, effect, linkedSignal, and signal-based inputs and queries.

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

const marks = signal(80);
const grade = computed(() => (marks() >= 75 ? 'A' : 'B'));

marks.update((m) => m + 5);
console.log(grade(), marks());

marks.set(60);
console.log(grade(), marks());

const list = signal<number[]>([60]);
list.update((l) => [...l, 65]);
console.log(list().length > 1 ? 'A' : 'B', list()[1]);

Run with Node 22.18 or newer (type stripping is on by default) after `npm i @angular/core`, using a package.json with `"type": "module"`, then `node main.ts`. Signals work outside components. Output: `A 85`, then `B 60`, then `A 65`.

Key points

  • Read with `s()`, write with `s.set(v)` or `s.update(fn)`.
  • `computed` is lazy, cached, and read-only.
  • Signals compare values with `Object.is`, so a mutated object or array does not trigger updates.
  • Templates must call the signal: `{{ count() }}`.

Common mistakes

  • Forgetting the parentheses in the template and printing the function instead of the value.
  • Mutating an array held in a signal with `push` and expecting the screen to update.
  • Trying to call `set` on a `computed`. It is read-only and does not have `set`.

Follow-ups

  • How is a signal different from an RxJS BehaviorSubject?
  • When does a computed recalculate?
  • What happens if you call `set` with the same primitive value?

Tips

  • Use a simple example such as marks and grade. Short code you can write live is worth more than theory.
  • Say that `computed` should have no side effects.
  • If asked about RxJS, say signals suit synchronous state and RxJS suits event streams over time.

Evaluation rubric

  • Calls signals as functions and uses set and update correctly.
  • Describes computed as lazy and cached.
  • Knows about immutable updates.