Signals help you keep track of changing data in your app simply and clearly.
Signal creation and reading in Angular
Start learning this pattern below
Jump into concepts and practice - no test required
import { signal } from '@angular/core'; const mySignal = signal(initialValue); // To read the current value: const currentValue = mySignal();
Use signal() to create a new signal with an initial value.
Call the signal like a function (mySignal()) to get its current value.
const count = signal(0); console.log(count()); // prints 0
const name = signal('Alice'); console.log(name()); // prints 'Alice'
const isVisible = signal(true); console.log(isVisible()); // prints true
This Angular component uses a signal to hold a counter value.
The template shows the current count by calling count().
Clicking the button calls increment() which updates the signal value.
The UI updates automatically when the signal changes.
import { Component, signal } from '@angular/core'; @Component({ selector: 'app-counter', standalone: true, template: ` <h2>Counter: {{ count() }}</h2> <button (click)="increment()">Add 1</button> ` }) export class CounterComponent { count = signal(0); increment() { this.count.set(this.count() + 1); } }
Signals are functions you call to get their current value.
Use set() method to update a signal's value.
Signals automatically notify Angular to update the UI when their value changes.
Signals store reactive values you can read by calling them like functions.
Use signal(initialValue) to create and signal() to read.
Updating signals triggers automatic UI updates in Angular components.
Practice
signal function do in Angular?Solution
Step 1: Understand the purpose of
Thesignalsignalfunction is used to create reactive values that Angular tracks for changes.Step 2: Identify what
It creates a value container that can be read by calling it and updated to trigger UI changes.signaldoesFinal Answer:
Creates a reactive value that can be read and updated -> Option CQuick Check:
Signal creation = reactive value container [OK]
- Confusing signal with component or service creation
- Thinking signal starts HTTP requests
- Assuming signal is for styling or templates
Solution
Step 1: Check the correct syntax for signal creation
The correct syntax uses lowercasesignalas a function with the initial value in parentheses.Step 2: Compare options
const count = signal(10); matches the correct syntax:const count = signal(10);. Others use wrong casing or invalid syntax.Final Answer:
const count = signal(10); -> Option AQuick Check:
signal() function call with initial value [OK]
- Using uppercase Signal instead of signal
- Assigning signal = 10 instead of calling signal(10)
- Trying to call a create method on signal
const count = signal(5); console.log(count()); count.set(10); console.log(count());
What will be printed in the console?
Solution
Step 1: Read the initial signal value
The signalcountis created with initial value 5, socount()returns 5.Step 2: Update the signal and read again
Callingcount.set(10)updates the value to 10, so the nextcount()returns 10.Final Answer:
5 then 10 -> Option AQuick Check:
Signal read before and after set() = 5, 10 [OK]
- Thinking count() returns the same value after set()
- Assuming set() is not a valid method
- Confusing signal reading with direct variable access
const name = signal('Alice');
console.log(name);
name('Bob');Solution
Step 1: Check how signal values are read and updated
Signals are read by calling them as functions:name(). To update, usename.set(newValue).Step 2: Identify the error in updating
The code tries to update the signal by callingname('Bob'), which is invalid. The correct way isname.set('Bob').Final Answer:
Signal must be updated using name.set('Bob') -> Option DQuick Check:
Update signals with set() method [OK]
- Trying to update signal by calling it as a function
- Forgetting parentheses when reading signal value
- Confusing signal with normal variables
const numbers = signal([1, 2, 3]); // Add 4 to numbers here
Solution
Step 1: Understand how to read and update signals
Read the current value by callingnumbers(). To update, usenumbers.set(newValue).Step 2: Add 4 to the existing array immutably
Create a new array with existing values plus 4:[...numbers(), 4]. Then update signal withnumbers.set([...numbers(), 4]).Final Answer:
numbers.set([...numbers(), 4]); -> Option BQuick Check:
Update signal immutably with set([...signal(), newItem]) [OK]
- Trying to call signal as a function to update
- Mutating array directly without set()
- Assigning new array to signal variable directly
