Sub topic 4
Reading Signals in Templates
This chapter assumes you know TypeScript classes and arrow functions, and that you have read the earlier chapters on component anatomy and interpolation. A signal is a wrapper around a value that tells Angular when the value changes. This chapter covers how templates read signals and the mistakes people make most often when they do.
Calling a signal to read it
You create a signal with signal(initialValue). To read the current value, you call the signal as a function. The same rule applies in the class and in the template.
import { Component, computed, signal } from '@angular/core';
@Component({
selector: 'app-cart-summary',
template: `
<p>Items: {{ prices().length }}</p>
<p>Total: {{ total() }}</p>
<button (click)="addItem()">Add a 5 item</button>
`
})
export class CartSummaryComponent {
prices = signal<number[]>([10, 25]);
total = computed(() => this.prices().reduce((sum, p) => sum + p, 0));
addItem() {
this.prices.update(list => [...list, 5]);
}
}When Angular renders this template, it records which signals each expression read. Here those are prices and total. When one of them changes, Angular marks the component for update and refreshes the bindings that depend on it. The (click) binding is covered in a later chapter. For now, all you need to know is that it calls addItem().
Derived values with computed()
computed() creates a read-only signal whose value comes from other signals. It recalculates only after one of its dependencies changes, and it caches the result between reads. Reading total() in several places therefore costs one calculation, not one per binding.
Writing to signals
| Method | Use it when |
|---|---|
| `set(value)` | You have the new value ready |
| `update(fn)` | The new value depends on the old one |
Templates usually only read signals. Writes go in class methods, which keeps the template simple.
Common errors
- Forgetting the parentheses.
{{ total }}prints the function itself, not the number. Write{{ total() }}. - Mutating in place.
this.prices().push(5)changes the array, but the signal still holds the same reference, so it does not notify anyone. Useupdateand return a new array. - Writing to a computed. A
computedsignal has nosetorupdatemethods. To change it, change the signals it reads. - Heavy logic in template methods. A plain method such as
remaining()runs every time the template is checked. If the work is expensive, usecomputedso the result is cached.
Exercise
A template shows {{ names().length }}, and a method runs this.names().push('Ada'). The count never changes on screen. Why, and what is the fix?
Show answer
push mutates the existing array, but the signal still holds the same reference, so it never signals a change. Replace the call with this.names.update(list => [...list, 'Ada']), which sets a new array and notifies the template.
Exercise
Add a computed signal named average to CartSummaryComponent and display it in the template.
Show answer
Declare average = computed(() => this.prices().length ? this.total() / this.prices().length : 0); and add <p>Average: {{ average() }}</p> to the template.