0
Angular · Advanced Angular
New technique
computed(): derived signals
A computed signal works out its value once and remembers it, until a signal it reads changes.
price = signal(40);
quantity = signal(3);
total = computed(() => this.price() * this.quantity());
// template: <p>Total: {{ total() }}</p>computed()builds a read-only signal from other signals.- It tracks every signal it reads (by calling it) and recalculates only when one of them changes.
- It's lazy: changing
priceonly markstotalas stale. Nothing runs until someone readstotal(). - The result is cached: reading
total()ten times calculates it once.