Sub topic 10
Using Signals in Templates
This chapter assumes you already know interpolation, property binding and event binding from earlier chapters, and that you can write a standalone Angular component in TypeScript. In that model, templates read plain class fields, and Angular has to check them to find out what changed. A signal is a reactive value container: it holds a value and tells Angular when that value changes. Templates read signals by calling them like functions.
Creating and reading a signal
You create a writable signal with signal(initialValue). To read it, you call it: count(). The call works the same way in the class and in the template, and the binding syntax does not change.
| API | Purpose | Example |
|---|---|---|
| `signal(v)` | Writable state | `count = signal(0)` |
| `sig()` | Read the current value | `{{ count() }}` |
| `sig.set(v)` | Replace the value | `count.set(0)` |
| `sig.update(fn)` | Derive the new value from the old one | `count.update(c => c + 1)` |
| `computed(fn)` | Read-only value derived from other signals | `double = computed(() => count() * 2)` |
A worked example
The cart below keeps one writable signal and derives two values from it. Interpolation, property binding and event binding all use the signals directly.
import { Component, computed, signal } from '@angular/core';
@Component({
selector: 'app-cart',
template: `
<p>Items: {{ count() }}</p>
<p>Total: {{ total() }}</p>
<button (click)="add(5)">Add $5 item</button>
<button [disabled]="count() === 0" (click)="clear()">Clear</button>
`,
})
export class CartComponent {
prices = signal<number[]>([]);
count = computed(() => this.prices().length);
total = computed(() => this.prices().reduce((sum, p) => sum + p, 0));
add(price: number) {
this.prices.update(list => [...list, price]);
}
clear() {
this.prices.set([]);
}
}A computed signal works out which signals it reads while it runs. It caches its result and recalculates only after one of those signals changes. Reading total() many times in a template therefore costs little.
How the template stays in sync
When a template calls a signal during rendering, Angular records that dependency. When set or update changes the signal, Angular marks the components that read it as needing an update and re-renders those bindings. The chapter on change detection covers the details. For now, the rule is that changing a signal through its API is enough to update the view.
Common errors
Forgetting the parentheses
{{ count }} renders the signal function, not its value. Depending on the Angular version, you will see function source text or a compiler diagnostic. Always write {{ count() }}.
Mutating the value in place
Signals compare values with Object.is by default. If you push into the existing array, the reference stays the same and nothing is notified:
// Wrong: same array reference, so no notification is sent
this.prices().push(5);
// Right: a new array, so readers are notified
this.prices.update(list => [...list, 5]);Treat objects and arrays held in signals as immutable, and replace them with new copies.
Writing to a computed signal
computed returns a read-only Signal, so it has no set or update. To change a derived value, change the signal it is derived from.
Exercises
Exercise
A template contains <p>{{ user.name }}</p> where user = signal({ name: 'Ada' }). Why does it fail, and how do you fix it?
Show answer
user is a signal, so you must call it to read its value. Write {{ user().name }}. To change the name, replace the whole object: this.user.update(u => ({ ...u, name: 'Grace' })).
Exercise
A handler runs this.tags().push('new'), but the list in the template does not change. Explain why and give the corrected code.
Show answer
The array reference stays the same, so the signal sees no change and does not notify the template. Use this.tags.update(t => [...t, 'new']) to store a new array.