Angular Signals: A Complete Deep Dive into Reactive State Management
Angular Signals: A Complete Deep Dive into Reactive State Management
import { signal } from '@angular/core';
const count = signal(0);
console.log(count()); // 0
count.set(5);
console.log(count()); // 5import { signal, computed } from '@angular/core';
const price = signal(100);
const tax = signal(0.18);
const total = computed(() => price() * (1 + tax()));
console.log(total()); // 118import { signal, effect } from '@angular/core';
const username = signal('Admin');
effect(() => {
console.log('Username changed to:', username());
});@Component({
selector: 'app-counter',
template: `
<p>Count: {{ count() }}</p>
<button (click)="increment()">+1</button>
`
})
export class CounterComponent {
count = signal(0);
increment() {
this.count.update(v => v + 1);
}
}Angular Signals were introduced in Angular 16 and have become a core part of Angular's reactivity model. This thread covers everything you need to know about Signals and how they change the way we manage state in Angular applications.
What Are Angular Signals?
Signals are a new reactive primitive that allow you to define reactive state in your components. Unlike RxJS Observables, Signals are synchronous, glitch-free, and deeply integrated into Angular's change detection system.
Creating a Signal
You can create a signal using the signal() function:
import { signal } from '@angular/core';
const count = signal(0);
console.log(count()); // 0
count.set(5);
console.log(count()); // 5import { signal, computed } from '@angular/core';
const price = signal(100);
const tax = signal(0.18);
const total = computed(() => price() * (1 + tax()));
console.log(total()); // 118import { signal, effect } from '@angular/core';
const username = signal('Admin');
effect(() => {
console.log('Username changed to:', username());
});@Component({
selector: 'app-counter',
template: `
<p>Count: {{ count() }}</p>
<button (click)="increment()">+1</button>
`
})
export class CounterComponent {
count = signal(0);
increment() {
this.count.update(v => v + 1);
}
}