What if your app could update all related values perfectly without you writing extra update code?
Why Computed signals for derived values in Angular? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a shopping cart app where the total price must update every time you add or remove items. You try to update the total manually each time you change the cart.
Manually updating totals is tricky and easy to forget. If you miss updating once, the total shows wrong values. It's slow and makes your code messy and hard to maintain.
Computed signals automatically calculate values based on other signals. When the cart changes, the total updates instantly and correctly without extra code.
let total = 0;
const cart = [];
function addItem(price) {
cart.push(price);
total += price; // manual update
}const cart = signal([]);
const total = computed(() => cart().reduce((sum, p) => sum + p, 0));It lets your app react instantly and correctly to changes, keeping derived values always in sync without extra effort.
In a budgeting app, computed signals keep your remaining balance updated automatically as you add expenses or income.
Manual updates are error-prone and hard to maintain.
Computed signals automatically track and update derived values.
This leads to cleaner, more reliable, and reactive code.
Practice
computed signal in Angular's signal system?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]
- Thinking computed signals store static data
- Confusing manual updates with automatic updates
- Assuming computed signals replace all signals
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]
- Using signal() instead of computed() for derived values
- Passing expressions directly without a function
- Not calling dependent signals as functions
count.set(5) is called?const count = signal(0); const double = computed(() => count() * 2); console.log(double()); count.set(5); console.log(double());
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]
- Assuming computed does not update after set()
- Forgetting to call signals as functions
- Expecting errors due to missing subscriptions
const price = signal(100); const tax = signal(0.1); const total = computed(() => price + tax * price); console.log(total());
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]
- Using signal variables directly without ()
- Thinking computed disallows arithmetic
- Ignoring signal initial values
firstName and lastName. Which code correctly updates the full name when either signal changes and avoids unnecessary recomputations?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]
- Using signal() instead of computed() for derived values
- Not calling signals as functions
- Forgetting space between names
