← Back to list

Signals and Effects in Angular: A Detailed Tutorial

Introduction

Anandeshi Rahul · 2024-05-20 22:00 · 0 claps · 2.0 min read
#angular-signals #angular-effects #angular-17 #react-hook
Open on Medium ↗
Wiki topics: 🌐 · Web Development

Signals and Effects in Angular: A Detailed Tutorial

Introduction

Signals and Effects are a new feature in Angular 17 that provides a more efficient and scalable way to manage state and side effects in your applications. This feature replaces the previous method of using Services and Observables to manage global state and side effects.

What are Signals and Effects?

Signals are a way to manage global state by emitting values to interested parties. Effects are a way to handle side effects, such as API calls or DOM manipulation, in a predictable and efficient manner.

Advantages over previous method

  • Simplifies state management and side effect handling
  • Improves performance by reducing unnecessary computations
  • Enhances code readability and maintainability
  • Provides a more scalable architecture for large-scale applications

How to use Signals and Effects

  • Create a Signal:
import { Signal } from '@angular/core';
const countSignal = new Signal(0);
  • Use the Signal in a Component:
import { Component, Signal } from '@angular/core';
@Component({
  selector: 'app-counter',
  template: '<p>Count: {{ count }}</p>'
})
export class CounterComponent {
  count = this.countSignal.value;
  constructor(private countSignal: Signal<number>) {}
}
  • Create an Effect:
import { Effect } from '@angular/core';
const fetchUserEffect = new Effect(() => {
  return fetch('https://api.example.com/user');
});
  • Use the Effect in a Component:
import { Component, Effect } from '@angular/core';
@Component({
  selector: 'app-user',
  template: '<p>User Name: {{ userName }}</p>'
})
export class UserComponent {
  userName = this.fetchUserEffect.value;
  constructor(private fetchUserEffect: Effect<{}>) {}
}

Real-Life Implementation: Building a Shopping Cart

Let’s build a shopping cart feature using Signals and Effects.

  • Create a cartSignal to manage the cart state:
import { Signal } from '@angular/core';
const cartSignal = new Signal([]);
  • Create a addToCartEffect to handle adding items to the cart:
import { Effect } from '@angular/core';
const addToCartEffect = new Effect((item: any) => {
  return cartSignal.update((cart) => [...cart, item]);
});
  • Use the cartSignal and addToCartEffect in your Components:
import { Component, Signal, Effect } from '@angular/core';
@Component({
  selector: 'app-cart',
  template: `
    <ul>
      <li *ngFor="let item of cart">{{ item.name }}</li>
    </ul>
    <button (click)="addToCart('New Item')">Add to Cart</button>
  `
})
export class CartComponent {
  cart = this.cartSignal.value;
  constructor(private cartSignal: Signal<any[]>, private addToCartEffect: Effect<{}>) {}
}

Code Examples

Example 1: Simple Signal

import { Signal } from '@angular/core';
const themeSignal = new Signal('light');

Example 2: Effect with API Call

import { Effect } from '@angular/core';
const fetchUserEffect = new Effect(() => {
  return fetch('https://api.example.com/user');
});

Example 3: Signal with Computation

import { Signal } from '@angular/core';
const totalPriceSignal = new Signal(0);
totalPriceSignal.compute((cart) => cart.reduce((acc, item) => acc + item.price, 0));

Conclusion

Signals and Effects provide a more efficient and scalable way to manage state and side effects in Angular applications. By using Signals and Effects, you can simplify your code, improve performance, and enhance code readability and maintainability. This feature is a significant improvement over the previous method of using Services and Observables, and is a must-know for any Angular developer.


메타데이터
post_id
847fdfc293e1
slug
signals-and-effects-in-angular-a-detailed-tutorial-847fdfc293e1
url
https://medium.com/@anandeshi.r/signals-and-effects-in-angular-a-detailed-tutorial-847fdfc293e1
canonical_url
https://medium.com/@anandeshi.r/signals-and-effects-in-angular-a-detailed-tutorial-847fdfc293e1
author_url
https://medium.com/@anandeshi.r
status
ok
fetched_at
2026-07-22 04:22:49