What are Angular Signals?

Introduced in Angular version 17 (currently in developer preview), Angular Signals offer a powerful mechanism for managing data changes and improving application reactivity. They address a key limitation of the traditional change detection system, which often performs unnecessary checks across all components, even when only a specific portion of the UI needs updating.

Benefits of Using Signals

Enhanced Performance: Signals enable Angular to pinpoint the exact components that require re-rendering based on data modifications. This targeted approach minimizes unnecessary computations and boosts overall application responsiveness.

Simplified Reactive Programming: Signals provide a more intuitive and lightweight alternative to RxJS for building reactive applications within Angular. Their API is easier to grasp, leading to cleaner and more maintainable code.

Core Concepts of Signals

  • Creating Signals: Utilize the signal() function to establish a signal with an initial value:
const mySignal = signal(0); // Creates a signal with a starting value of 0
  • Reading Values: Call the signal as if it were a function to retrieve its current value
const currentValue = mySignal(); // Fetches the value stored in mySignal
  • Updating Values: Employ the set() or update() methods to modify signal values:
mySignal.set(10); // Directly sets the value to 10
mySignal.update((val) => val + 1); // Updates the value using a callback function
  • Computed Signals: Derive new signals based on the values of existing signals:
const doubledSignal = computed(() => mySignal() * 2); // Creates a computed signal that doubles mySignal's value
  • Effects: Define side effects that should be executed in response to signal changes:
effect(() => {
  console.log('Signal value changed:', mySignal());
})

Illustrative Example

Consider a scenario where you have a counter component in your Angular application. Traditionally, changing the counter value would trigger a full re-render of the entire application, even if only the counter section needs to be updated.

With Signals, you can create a count signal and leverage it within the counter component’s template:

// counter.component.ts
import { signal, computed } from '@angular/core';

const count = signal(0);
const doubledCount = computed(() => count() * 2);

// counter.component.html
<p>Count: {{ count() }}</p>
<p>Doubled Count: {{ doubledCount() }}</p>

<button (click)="increment()">Increment</button>

increment() {
  count.update((val) => val + 1);

}

In this example, modifying the count signal via the increment() method only re-renders the portions of the template that rely on count and doubledCount, leading to more efficient rendering.

Additional Considerations

  • While Signals are currently in developer preview (as of Angular 17), you can experiment with them to gain familiarity and potentially improve your application’s performance in the future.
  • Keep in mind that Signals are not a replacement for RxJS; they serve different purposes. RxJS offers a broader set of reactive programming operators, while Signals focus on streamlined data change management within Angular.

Quiz game: