Signals help Angular track changes in data easily and update the screen quickly. They make apps faster and simpler to build.
Why signals are introduced 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
import { signal } from '@angular/core'; const count = signal(0); count.set(1); // update value const current = count(); // read value
Use signal(initialValue) to create a signal with a starting value.
Call the signal like a function count() to get its current value.
Examples
Angular
const name = signal('Alice'); console.log(name()); // prints 'Alice' name.set('Bob'); console.log(name()); // prints 'Bob'
update to change the signal value based on the old value.Angular
const counter = signal(0); counter.update(value => value + 1); console.log(counter()); // prints 1
Sample Program
This Angular component uses a signal to keep track of a counter. When you click the button, the counter increases and the screen updates automatically.
Angular
import { Component, signal } from '@angular/core'; @Component({ selector: 'app-counter', standalone: true, template: ` <h1>Counter: {{ counter() }}</h1> <button (click)="increment()">Add 1</button> ` }) export class CounterComponent { counter = signal(0); increment() { this.counter.update(value => value + 1); } }
Important Notes
Signals replace some complex change detection methods with a simpler reactive approach.
They help Angular know exactly what changed, so it updates only those parts of the UI.
Signals work well with Angular's new standalone components and modern patterns.
Summary
Signals make it easy to track and react to data changes.
They improve app speed by updating only what is needed.
Signals simplify writing reactive Angular code.
Practice
1. Why were signals introduced in Angular?
easy
Solution
Step 1: Understand the purpose of signals
Signals help Angular track changes in data and update the UI efficiently.Step 2: Compare with other options
Replacing directives, removing components, or simplifying CSS are unrelated to signals.Final Answer:
To make it easier to track and react to data changes -> Option AQuick Check:
Signals improve data tracking = C [OK]
Hint: Signals help track data changes easily [OK]
Common Mistakes:
- Thinking signals replace Angular directives
- Confusing signals with styling tools
- Believing signals remove components
2. Which of the following is the correct way to create a signal in Angular?
easy
Solution
Step 1: Recall Angular signal syntax
The correct syntax uses the functionsignal()to create a signal.Step 2: Check other options
Options B, C, and D use incorrect constructors or methods not in Angular's signal API.Final Answer:
const count = signal(0); -> Option CQuick Check:
Use signal() function to create signals = A [OK]
Hint: Use signal() function to create signals [OK]
Common Mistakes:
- Using new keyword with Signal
- Calling createSignal instead of signal
- Using dot notation like signal.create
3. Given this Angular code snippet:
What will be printed in the console?
const count = signal(0); count.set(5); console.log(count());
What will be printed in the console?
medium
Solution
Step 1: Understand signal creation and update
The signalcountstarts at 0, thencount.set(5)updates its value to 5.Step 2: Evaluate the console.log output
Callingcount()returns the current value, which is 5 after the update.Final Answer:
5 -> Option AQuick Check:
Signal value after set(5) = 5 [OK]
Hint: Calling signal() returns current value [OK]
Common Mistakes:
- Thinking count() returns the initial value
- Confusing signal object with its value
- Expecting undefined without set call
4. What is wrong with this Angular signal code?
const count = signal(0); count = signal(5);
medium
Solution
Step 1: Understand signal immutability
Signals are constants; you cannot reassign the variable holding a signal.Step 2: Check other options
signal() accepts numbers, and count() is for reading value, not reassignment.Final Answer:
Signals cannot be reassigned like this -> Option DQuick Check:
Signal variables are constant references = A [OK]
Hint: Signals are constants; don't reassign them [OK]
Common Mistakes:
- Trying to reassign signal variables
- Confusing signal creation with value reading
- Thinking signal only accepts strings
5. How do signals improve Angular app performance compared to traditional change detection?
hard
Solution
Step 1: Understand traditional change detection
Traditional Angular runs change detection on many components, which can be slow.Step 2: Understand signals' selective update
Signals update only UI parts that depend on changed data, improving speed and efficiency.Step 3: Evaluate options
By updating only the parts of the UI that depend on changed data correctly describes signals' selective update. By disabling all UI updates until manual refresh is incorrect because signals do not disable UI updates.Final Answer:
By updating only the parts of the UI that depend on changed data -> Option BQuick Check:
Signals update only needed UI parts = B [OK]
Hint: Signals update only affected UI parts [OK]
Common Mistakes:
- Thinking signals disable UI updates
- Believing change detection runs everywhere always
- Assuming signals remove templates
