Sub topic 2
A quick refresher on signals
This chapter assumes you know TypeScript classes, can write an Angular standalone component with a template, and have seen a signal read by calling it. It covers only the parts of signals you need for the rest of the tutorial: reading, computed(), and effect(). Signal inputs are signals too, so everything here applies to them.
What a signal is
A signal is a wrapper around a value. It tells Angular when that value changes. You create a writable signal with signal() and read it by calling it like a function.
import { signal } from '@angular/core';
const quantity = signal(1);
console.log(quantity()); // 1
quantity.set(3);
quantity.update((q) => q + 1);
console.log(quantity()); // 4set(value)replaces the value.update(fn)computes the new value from the old one.- Calling
quantity()returns the current value. In a template or inside a reactive function, this call also records a dependency.
The call matters. Writing {{ quantity }} shows the signal object, while {{ quantity() }} shows the value.
Derived values with computed()
A computed signal calculates its value from other signals. It is read-only, so it has no set() or update(). It is lazy, which means the function runs the first time someone reads it. It is also cached, so the result is reused until one of the signals it read changes.
Side effects with effect()
An effect runs a function once and then runs it again whenever any signal it read changes. Use it for work that leaves Angular's data model, such as logging, writing to localStorage, or calling a non-Angular library. Do not use it to compute values that another part of your component needs. That is the job of computed().
An effect must be created in an injection context. The simplest place is a component's constructor or a field initializer.
import { Component, computed, effect, signal } from '@angular/core';
@Component({
selector: 'app-price',
template: `
<p>Quantity: {{ quantity() }}</p>
<p>Total: {{ total() }}</p>
<button (click)="add()">Add one</button>
`,
})
export class Price {
quantity = signal(1);
unitPrice = 5;
total = computed(() => this.quantity() * this.unitPrice);
constructor() {
effect(() => {
console.log(`Total is now ${this.total()}`);
});
}
add() {
this.quantity.update((q) => q + 1);
}
}Each click on the button changes quantity. That invalidates total. The template and the effect, which both read total, are then refreshed.
Dependencies are tracked automatically
You never list dependencies by hand. Angular tracks the signals that a computed or effect function reads while it runs. If a branch of your code does not run, signals in that branch are not tracked. This is usually what you want, but it means a signal read only inside an if can stop mattering when the condition becomes false.
Why this matters for inputs
input() returns a read-only signal. A child component reads it by calling it, and it can build computed() values and effect() calls on top of it. The next chapters rely on three consequences of this:
- You read an input with
name(), nevername. - You cannot call
set()on an input. The parent owns the value. - Anything derived from an input should be a
computed(), so it stays current when the parent passes a new value.
Exercise
In the Price component, the user clicks "Add one" twice, starting from quantity = 1 and unitPrice = 5. What does the last console message say, and why can you not call total.set(20) in this component?
Show answer
The last message is "Total is now 15", because quantity becomes 3 and 3 × 5 = 15. total is a computed signal, so it is read-only and has no set() method. To change the total, change quantity, and total recalculates on its own.