0
0
Angularframework~10 mins

Signal creation and reading in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Signal creation and reading
Create signal with initial value
Signal holds current value
Read signal value anywhere
Use value in template or logic
If signal changes -> dependent code updates
This flow shows how a signal is created with a starting value, then read anywhere in the code or template, and how changes to the signal update dependent parts automatically.
Execution Sample
Angular
import { signal } from '@angular/core';

const count = signal(0);

console.log(count());
count.set(5);
console.log(count());
This code creates a signal with 0, reads and logs it, updates it to 5, then reads and logs the new value.
Execution Table
StepActionSignal Value BeforeOperationSignal Value AfterOutput
1Create signal with 0N/Asignal(0)0No output
2Read signal value0count()00
3Update signal value0count.set(5)5No output
4Read updated signal value5count()55
5End of code5N/A5Execution stops
💡 Signal value is 5, no more code to execute
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
countN/A00555
Key Moments - 2 Insights
Why do we call count() to read the signal value instead of just using count?
Signals in Angular are functions. Calling count() executes the function to get the current value. See execution_table rows 2 and 4 where count() returns the value.
What happens when we use count.set(5)? Does it change the variable directly?
No, count.set(5) updates the internal value inside the signal. The variable count remains a function. This is shown in execution_table row 3 where the value changes from 0 to 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 4 when reading the signal?
A5
B0
Cundefined
DError
💡 Hint
Check the Output column at step 4 in the execution_table
At which step does the signal value change from 0 to 5?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at Signal Value After column in execution_table rows 2 and 3
If we forget to call count() and just log count, what would happen?
AIt logs the current value
BIt logs the signal function itself
CIt throws an error
DIt logs undefined
💡 Hint
Signals are functions, so logging count without () logs the function, not the value
Concept Snapshot
Signal creation and reading in Angular:
- Use signal(initialValue) to create a signal.
- Signals are functions; call them like count() to read value.
- Use count.set(newValue) to update the signal.
- Reading a signal triggers reactive updates.
- Signals hold state and notify dependents on change.
Full Transcript
In Angular, a signal is created by calling signal() with an initial value. This returns a function that holds the current value. To read the value, you call the signal function like count(). To update the value, you use count.set(newValue). When the signal changes, Angular automatically updates any code or template parts that depend on it. This example shows creating a signal with 0, reading it, updating to 5, and reading again. The key is remembering signals are functions, so you must call them to get the value.