Sub topic 5
Computing values from inputs
This chapter assumes you know TypeScript classes, Angular standalone components, property and event binding, and that you read a signal by calling it. It covers deriving state from signal inputs with computed().
Why derive instead of copy
A signal input is a read-only signal. When the parent binds a new value, the input updates and everything that reads it is notified. A common mistake is copying an input into a plain field, for example in the constructor. The copy keeps the first value and never changes.
computed() avoids this. It creates a read-only signal whose value comes from other signals. Angular tracks which signals the function reads. When one of them changes, the computed value is marked stale and is recalculated the next time something reads it. The result is cached, so repeated reads cost nothing until a dependency changes.
A first example
The Avatar component below turns a full name into initials.
import { Component, computed, input } from '@angular/core';
@Component({
selector: 'app-avatar',
template: `<span class="avatar">{{ initials() }}</span>`,
})
export class Avatar {
name = input.required<string>();
initials = computed(() =>
this.name()
.split(' ')
.map((part) => part[0])
.join('')
);
}initials reads name(), so it depends on the input. If the parent changes name from Ada Lovelace to Grace Hopper, initials goes from AL to GH with no extra code.
The parent only binds the input:
<app-avatar [name]="user.name" />How the update flows
Combining several inputs
A computed value can read more than one input. It recalculates when any of them changes.
import { Component, computed, input } from '@angular/core';
@Component({
selector: 'app-price-tag',
template: `<p>{{ label() }}</p>`,
})
export class PriceTag {
price = input.required<number>();
quantity = input(1);
currency = input('USD');
total = computed(() => this.price() * this.quantity());
label = computed(() =>
new Intl.NumberFormat('en-US', {
style: 'currency',
currency: this.currency(),
}).format(this.total())
);
}label reads total, which reads price and quantity. Computed signals can depend on other computed signals. Angular recalculates only the parts affected by a change.
Rules for computed functions
- Keep the function pure. Calculate and return a value. Do not set other signals or call services that change state.
- Read signals inside the function. Angular only tracks signals that are read while the function runs. A value taken from a plain field will not trigger updates.
- Do not try to write to a computed signal. It has no
set()orupdate(). - Handle empty values when an input is optional. If
namecould be an empty string,part[0]would beundefinedfor an empty part, so filter first.
A safer version of the initials logic:
initials = computed(() =>
this.name()
.split(' ')
.filter((part) => part.length > 0)
.map((part) => part[0])
.join('')
);Computed versus a method
A template can call a method such as getInitials(), and that works. The method runs on every change detection pass. A computed signal runs only when a dependency changes. For anything beyond trivial work, prefer computed().
| Approach | Recalculates | Tracks input changes |
|---|---|---|
| Plain field set once | Never | No |
| Method called in template | Every change detection pass | Yes, by rerunning |
| `computed()` | Only when a dependency changes | Yes |
Exercise
A component declares first = input('') and last = input(''). Write a computed() field named fullName that joins them with a space. Why does it update when the parent changes only last?
Show answer
fullName = computed(() => ${this.first()} ${this.last()}); The function reads both inputs, so Angular records both as dependencies. A change to last marks fullName stale, and the next read recalculates it using the current value of each input.