0
0
Angularframework~10 mins

Computed signals for derived values in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Computed signals for derived values
Create base signals
Define computed signal
Access computed signal
Base signal changes?
NoUse cached value
Yes
Recompute derived value
Update computed signal value
This flow shows how base signals are created, a computed signal is defined from them, and how changes in base signals trigger recomputation of the derived value.
Execution Sample
Angular
const count = signal(1);
const doubleCount = computed(() => count() * 2);
console.log(doubleCount());
count.set(3);
console.log(doubleCount());
This code creates a base signal 'count', a computed signal 'doubleCount' that doubles 'count', then logs the computed value before and after changing 'count'.
Execution Table
StepActioncount valuedoubleCount valueExplanation
1Initialize count with 11undefinedBase signal 'count' starts at 1, computed not evaluated yet
2Define doubleCount as count * 21undefinedComputed signal created but not yet accessed
3Access doubleCount()12Computed runs: 1 * 2 = 2, caches value 2
4Set count to 332Base signal changes, computed cache invalidated
5Access doubleCount() again36Computed re-runs: 3 * 2 = 6, updates cache
💡 Execution stops after logging updated computed value; no further changes.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5
count1133
doubleCountundefined22 (cache invalidated)6
Key Moments - 2 Insights
Why does doubleCount not update immediately when count changes?
The computed signal caches its value until accessed again. When 'count' changes (Step 4), the cache is invalidated but 'doubleCount' only recomputes when called next (Step 5). See execution_table rows 4 and 5.
What happens if we never call doubleCount() after count changes?
The computed signal's value stays stale because it only recalculates when accessed. So changes in base signals alone don't trigger recomputation until the computed signal is read.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of doubleCount after Step 3?
A1
Bundefined
C2
D3
💡 Hint
Check the 'doubleCount value' column at Step 3 in the execution_table.
At which step does the computed signal cache get invalidated?
AStep 2
BStep 4
CStep 5
DStep 3
💡 Hint
Look for when 'count' changes and cache is marked invalid in the execution_table.
If we never call doubleCount() after changing count, what happens to doubleCount's value?
AIt stays the old cached value
BIt resets to undefined
CIt updates automatically
DIt throws an error
💡 Hint
Refer to key_moments explanation about computed signals caching behavior.
Concept Snapshot
Computed signals derive values from base signals.
They cache results until base signals change.
When a base signal changes, cache invalidates.
Computed recomputes only when accessed next.
Use computed() to define derived reactive values.
Full Transcript
In Angular, computed signals let you create values derived from other signals. First, you create base signals with signal(). Then, you define a computed signal using computed() that depends on those base signals. When you access the computed signal, it runs its function and caches the result. If a base signal changes, the computed signal's cache is invalidated but it does not recompute immediately. It waits until you access it again, then it recalculates using the latest base signal values. This behavior helps optimize performance by avoiding unnecessary recalculations. The example code shows count starting at 1, doubleCount doubling it, then count changes to 3. The computed signal updates only when accessed after the change. This step-by-step trace helps beginners see how Angular's computed signals work internally.