Computed signals help you automatically update values when other values change. They keep your app data fresh without extra work.
Computed signals for derived values in Angular
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Angular
const derivedValue = computed(() => { // return a value based on other signals });
The computed function takes a function that returns a value.
It automatically tracks signals used inside and updates when they change.
Examples
count and a computed signal doubleCount that is always twice the count.Angular
const count = signal(0); const doubleCount = computed(() => count() * 2);
Angular
const firstName = signal('Jane'); const lastName = signal('Doe'); const fullName = computed(() => `${firstName()} ${lastName()}`);
Sample Program
This Angular component shows a shopping cart with an item price and quantity. The total price is a computed signal that updates automatically when quantity changes. Clicking the button increases quantity and updates total.
Angular
import { Component, signal, computed } from '@angular/core'; import { CommonModule } from '@angular/common'; @Component({ selector: 'app-root', standalone: true, imports: [CommonModule], template: ` <h1>Shopping Cart</h1> <p>Item price: {{ itemPrice() | currency }}</p> <p>Quantity: {{ quantity() }}</p> <p><strong>Total: {{ total() | currency }}</strong></p> <button (click)="increase()">Add One</button> ` }) export class AppComponent { itemPrice = signal(10); quantity = signal(1); total = computed(() => this.itemPrice() * this.quantity()); increase() { this.quantity.update(q => q + 1); } }
Important Notes
Computed signals only recalculate when their dependencies change, making them efficient.
Use computed inside components or services to keep derived data reactive.
Summary
Computed signals automatically update derived values when dependencies change.
They simplify keeping your UI and data in sync without manual updates.
Practice
1. What is the main purpose of a
computed signal in Angular's signal system?easy
Solution
Step 1: Understand what computed signals do
Computed signals derive their value from other signals and update automatically when those signals change.Step 2: Compare options with this behavior
Only To automatically update a value based on other signals when they change describes automatic updates based on dependencies, which matches computed signals.Final Answer:
To automatically update a value based on other signals when they change -> Option CQuick Check:
Computed signals = auto-update derived values [OK]
Hint: Computed signals auto-update when dependencies change [OK]
Common Mistakes:
- Thinking computed signals store static data
- Confusing manual updates with automatic updates
- Assuming computed signals replace all signals
2. Which of the following is the correct syntax to create a computed signal in Angular?
easy
Solution
Step 1: Recall computed signal syntax
Computed signals usecomputed(() => ...)with a function returning the derived value.Step 2: Check each option
const total = computed(() => price() + tax()); correctly usescomputed(() => price() + tax()). const total = signal(() => price + tax); wrongly usessignaland no function. const total = computed(price + tax); misses the function wrapper. const total = signal(price() + tax()); usessignalinstead ofcomputed.Final Answer:
const total = computed(() => price() + tax()); -> Option AQuick Check:
Computed syntax = computed(() => value) [OK]
Hint: Use computed(() => ...) with a function for derived values [OK]
Common Mistakes:
- Using signal() instead of computed() for derived values
- Passing expressions directly without a function
- Not calling dependent signals as functions
3. Given the code below, what will be logged after
count.set(5) is called?const count = signal(0); const double = computed(() => count() * 2); console.log(double()); count.set(5); console.log(double());
medium
Solution
Step 1: Evaluate initial values
Initially,countis 0, sodouble()returns 0 * 2 = 0.Step 2: After
Settingcount.set(5)countto 5 updatesdoubleautomatically. Callingdouble()now returns 5 * 2 = 10.Final Answer:
0 then 10 -> Option AQuick Check:
Initial double=0, after update=10 [OK]
Hint: Computed updates automatically after signal changes [OK]
Common Mistakes:
- Assuming computed does not update after set()
- Forgetting to call signals as functions
- Expecting errors due to missing subscriptions
4. Identify the error in the following code snippet using computed signals:
const price = signal(100); const tax = signal(0.1); const total = computed(() => price + tax * price); console.log(total());
medium
Solution
Step 1: Check how signals are accessed
Signals are functions and must be called with()to get their current value.Step 2: Analyze the computed expression
The expression usespriceandtaxdirectly without calling them, so it uses the signal objects, not their values.Final Answer:
Signals must be called as functions inside computed -> Option BQuick Check:
Access signals with () inside computed [OK]
Hint: Always call signals as functions inside computed [OK]
Common Mistakes:
- Using signal variables directly without ()
- Thinking computed disallows arithmetic
- Ignoring signal initial values
5. You want to create a computed signal that returns the full name by combining two signals:
firstName and lastName. Which code correctly updates the full name when either signal changes and avoids unnecessary recomputations?hard
Solution
Step 1: Understand the goal
The computed signal should combinefirstNameandlastNamesignals and update automatically when either changes.Step 2: Evaluate each option
const fullName = computed(() => `${firstName()} ${lastName()}`); correctly calls both signals as functions and concatenates with a space. const fullName = signal(`${firstName()} ${lastName()}`); usessignalwhich won't update automatically. const fullName = computed(() => firstName + ' ' + lastName); uses signal variables directly without calling them. const fullName = computed(() => firstName() + lastName()); concatenates without space.Final Answer:
const fullName = computed(() => `${firstName()} ${lastName()}`); -> Option DQuick Check:
Call signals with () and combine with space [OK]
Hint: Use computed with template literals calling signals () [OK]
Common Mistakes:
- Using signal() instead of computed() for derived values
- Not calling signals as functions
- Forgetting space between names
