Home >Web Front-end >JS Tutorial >Understanding Angular Signals: A Comprehensive Guide
This guide explores Angular Signals, a modern reactive programming paradigm simplifying state management. We'll examine their core functionality, including writable and computed signals, and demonstrate their use with effects. Practical examples will solidify your understanding.
Understanding Angular Signals
Angular Signals are a reactive primitive designed for more predictable and manageable state. Unlike older methods (Observables, EventEmitters), Signals offer a cleaner approach to reactive variables, automatically updating the view on state changes.
Key Signal Attributes:
Writable Signals: The Basics
Writable Signals are the fundamental building block, representing mutable reactive state.
Creating a Writable Signal:
Use the signal()
function:
<code class="language-typescript">import { signal } from '@angular/core'; export class CounterComponent { counter = signal(0); increment() { this.counter.update(val => val + 1); } decrement() { this.counter.update(val => val - 1); } }</code>
Writable Signal Methods:
set(newValue)
: Directly assigns a new value. this.counter.set(10);
update(callback)
: Updates using a callback function. this.counter.update(val => val * 2);
mutate(callback)
: Directly mutates objects or arrays. this.arraySignal.mutate(arr => arr.push(5));
Computed Signals: Derived Values
Computed Signals derive their value from other signals, automatically updating when dependencies change. They are read-only.
Defining a Computed Signal:
Use the computed()
function:
<code class="language-typescript">import { signal, computed } from '@angular/core'; export class PriceCalculator { price = signal(100); quantity = signal(2); totalPrice = computed(() => this.price() * this.quantity()); }</code>
Updating price
or quantity
automatically recalculates totalPrice
.
Effects: Managing Side Effects
Effects execute side effects (logging, API calls, DOM manipulation) when signals change.
Creating an Effect:
Use the effect()
function:
<code class="language-typescript">import { signal, effect } from '@angular/core'; export class EffectExample { counter = signal(0); constructor() { effect(() => console.log('Counter:', this.counter())); } increment() { this.counter.update(val => val + 1); } }</code>
Effect Use Cases:
Complete Example: A Counter App
This counter app demonstrates writable, computed signals, and effects:
<code class="language-typescript">import { Component, signal, computed, effect } from '@angular/core'; @Component({ selector: 'app-counter', template: ` <div> <p>Counter: {{ counter() }}</p> <p>Double: {{ doubleCounter() }}</p> <button (click)="increment()">Increment</button> <button (click)="decrement()">Decrement</button> </div> ` }) export class CounterComponent { counter = signal(0); doubleCounter = computed(() => this.counter() * 2); constructor() { effect(() => console.log('Counter changed:', this.counter())); } increment() { this.counter.update(val => val + 1); } decrement() { this.counter.update(val => val - 1); } }</code>
Signal Best Practices:
computed()
.update()
over mutate()
.Conclusion
Angular Signals provide a modern, efficient approach to reactive state management. Their simplicity and capabilities enhance developer experience and application performance, leading to more maintainable and predictable Angular code.
The above is the detailed content of Understanding Angular Signals: A Comprehensive Guide. For more information, please follow other related articles on the PHP Chinese website!