3 min read
Angular Signals: A Practical Guide with Real-World Examples
What Angular Signals are and how they change reactivity in Angular, with the practical patterns and examples worth knowing.
Angular / Angular Signals / Reactivity / Frontend Architecture / TypeScript / Best Practices
Angular Signals completely change how we manage state and build reactive UIs. They provide a simpler, faster, and more predictable reactivity model compared to RxJS and traditional change detection.
This guide will help you understand signals with clear examples, real-world patterns, and best practices used in enterprise-scale applications.
Table of Contents
- What Are Signals?
- Creating and Using Signals
- Computed Signals
- Effect Signals
- Signal-Based Services
- Signals vs RxJS — When to Use What
- Real-World Use Cases
- Best Practices
What Are Signals?
Signals are reactive values that notify the template when they change.
Think of a signal as:
- A variable
- That automatically updates the UI
- Without manual subscriptions
- Without async pipes
- Without cleanup code
Example:
const count = signal(0);
count(); // get value → 0
count.set(5); // update value
count.update((prev) => prev + 1); // update with logic
Creating and Using Signals
Component Example
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-counter',
standalone: true,
template: \`
<h2>Counter Example</h2>
<p>Current Count: {{ count() }}</p>
<button (click)="increment()">Increment</button>
<button (click)="reset()">Reset</button>
\`
})
export class CounterComponent {
count = signal(0);
increment() {
this.count.update(c => c + 1);
}
reset() {
this.count.set(0);
}
}
Computed Signals
import { Component, signal, computed } from '@angular/core';
@Component({
selector: 'app-cart',
standalone: true,
template: \`
<h2>Cart Summary</h2>
<p>Total Items: {{ items().length }}</p>
<p>Total Price: ₹{{ totalPrice() }}</p>
\`
})
export class CartComponent {
items = signal([
{ name: 'Laptop', price: 50000 },
{ name: 'Mouse', price: 1200 }
]);
totalPrice = computed(() =>
this.items().reduce((sum, item) => sum + item.price, 0)
);
}
Effect Signals
import { Component, signal, effect } from '@angular/core';
@Component({
selector: 'app-effect-demo',
standalone: true,
template: \`
<input [(ngModel)]="name()" (ngModelChange)="name.set($event)" />
<p>Hello, {{ name() }}!</p>
\`
})
export class EffectDemoComponent {
name = signal('Dev');
constructor() {
effect(() => {
console.log('Name updated:', this.name());
});
}
}
Signal-Based Services
import { Injectable, signal, computed } from '@angular/core';
interface User {
id: number;
name: string;
role: string;
}
@Injectable({ providedIn: 'root' })
export class UserStore {
private readonly _user = signal<User | null>(null);
readonly user = computed(() => this._user());
readonly isAdmin = computed(() => this._user()?.role === 'admin');
login(user: User) {
this._user.set(user);
}
logout() {
this._user.set(null);
}
}
Signals vs RxJS
| Feature | Signals | RxJS |
|---|---|---|
| Template reactivity | ⭐ Best | Requires async pipe |
| Managing single values | ⭐ Best | Overkill |
| Complex streams | ❌ Not ideal | ⭐ Best |
| Operators | Limited | Huge set |
| Data fetching | Good | ⭐ Excellent |
| Component state | ⭐ Best | Good |
| Cross-component state | ⭐ Great | Great |
Real-World Use Cases
- Form State
- Dark Mode Toggle
- Pagination
- Shopping Cart
- Modal State
Best Practices
- Use readonly computed signals
- Use signals for UI state
- Prefer
update()for immutability - Avoid deep nested signal objects
- Use effects only for side effects
Conclusion
Angular Signals make reactivity simpler, faster, and more predictable. They are perfect for managing UI state and improving application performance. Combine Signals with RxJS for powerful hybrid architectures.
Happy coding! 🚀