Angular Signals
There is no shortage of articles about Angular signals. But here’s another one. Signals are one of the most popular new Angular features to be introduced recently. The description of signals from the documentation can’t really be improved:
Angular Signals is a system that granularly tracks how and where your state is used throughout an application, allowing the framework to optimize rendering updates.
Signals are basically just properties that let you know when they change.
I think there are two reasons why signals are important:
- They are easier to learn and use than observables and are adequate for most use cases.
- They allow Angular to be more granular about change detection which is more performant and might lead to the eventual removal of zone-based change detection.
Let’s dive in.
The Simplest Possible Example
I’m starting with a brand new Angular 17 application.
You create a signal by calling the signal() function:
import { signal } from '@angular/core';
.
.
cartItems = signal(0);You then read the value in the template like so:
My Cart: {{ cartItems() }}Probably the two biggest things to get used to are contained right there in this simple example:
- You call a function to instantiate the signal (
signal(0)). - A signal is a function, so you call it to read it (
cartItems()).
Updating the Signal
If the signal is writable (which it is by default) you can update it in one of two ways:
this.cartItems.set(1);
//OR
this.cartItems.update((value) => value + 1);Note that My Cart: {{ cartItems() }} will update every time the value changes — you don’t have to do anything else.
Computed Signal
A computed signal is just like a computed property or a computed database column. Its value is derived from one or more other signals. If we pretend we only sell one item and it costs $10, we can create a subtotal computed signal as follows:
import { computed } from '@angular/core';
.
.
subTotal = computed(() => {
return this.cartItems() * 10;
});Note that the computed function does not take any parameters. It just references the input signal(s) like any other function in the class would.
Effects
The last feature of signals that we will discuss are effects. Effects allow you to respond and perform actions when a signal value changes. The following effect writes a message to the console every time the cartItems signal is updated:
effect(() => {
console.log('Cart Items Updated:', this.cartItems());
});Note that we are not subscribing to cartItems or anything of that sort. Just referencing it is enough to make the effect monitor it for changes.
Also note that as written above, the effect would have to be put in the constructor so that it has access to an injection context.
Get Sean Haddock’s stories in your inbox
Join Medium for free to get updates from this writer.
To place the effect outside of the constructor you could assign it to a field:
cartItemsUpdated = effect(() => {
console.log('Cart Items Updated:', this.cartItems());
});Can’t we do all this with properties?
Yes and no. We can achieve what we have done with properties. Here is a full example of one way you could do it (with additional code to make the cartItems signal update at an interval):
import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet],
template: `My Cart: {{ cartItems }} Sub Total: {{ subTotal }}`,
styleUrl: './app.component.scss',
})
export class AppComponent implements OnInit {
#cartItems = 0;
get cartItems() {
return this.#cartItems;
}
set cartItems(value) {
this.#cartItems = value;
console.log('Cart Items Updated:', value);
}
get subTotal() {
return this.cartItems * 10;
}
ngOnInit() {
let counter = 0;
const intervalId = setInterval(() => {
counter++;
this.cartItems = counter;
}, 1000);
}
}This works…until you implement OnPush change detection. Setting change detection to OnPush allows Angular change detection to run less often, resulting in more performant applications. From Angular:
Change detection is sufficiently fast for most applications. However, when an application has an especially large component tree, running change detection across the whole application can cause performance issues. You can address this by configuring change detection to only run on a subset of the component tree.
If you are confident that a part of the application is not affected by a state change, you can use OnPush to skip change detection in an entire component subtree.
Let’s implement OnPush and see what happens:
import { ChangeDetectionStrategy } from '@angular/core';
.
.
@Component({
.
.
changeDetection: ChangeDetectionStrategy.OnPush,
})Now it no longer works (template does not update), unless you manually tell Angular to run the change detection cycle:
import { ChangeDetectorRef } from '@angular/core';
.
.
#changeDetectorRef = inject(ChangeDetectorRef);
.
.
set cartItems(value) {
.
.
this.#changeDetectorRef.markForCheck();
.
.
}Now the template is updating again.
But if we use signals, we don’t have to do those extra steps — it just works.
Here is the same solution implemented with signals:
import {
Component,
OnInit,
computed,
signal,
effect,
ChangeDetectionStrategy,
} from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet],
template: `My Cart: {{ cartItems() }} Sub Total: {{ subTotal() }}`,
styleUrl: './app.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent implements OnInit {
cartItems = signal(0);
subTotal = computed(() => {
return this.cartItems() * 10;
});
constructor() {
effect(() => {
console.log('Cart Items Updated:', this.cartItems());
});
}
ngOnInit() {
let counter = 0;
const intervalId = setInterval(() => {
counter++;
this.cartItems.set(counter);
}, 1000);
}
}And here is the diff between the two:
Conclusion
So, should you start using signals? Yes (on Angular 17+).
While you could argue that it’s not that much easier/less verbose than properties (or even RxJS if you’re already very familiar with it), or that your application doesn’t really need OnPush change detection, I would argue:
- I think it’s clear that signals are at least somewhat simpler/less verbose under certain scenarios, and techniques that are simpler and less verbose tend to win in the long run.
- Maybe you don’t have to implement OnPush, but why not make your application more performant if the development cost is very low?
- I think it’s possible that in the future you will be required to use signals in order to achieve reactivity in Angular. I think the fine-grained reactivity that signals provide are going to allow the Angular team to introduce new powerful features that will be dependent upon signals.
Signals are basically just properties that let you know when they change.
Bibliography
Stackademic 🎓
Thank you for reading until the end. Before you go:
- Please consider clapping and following the writer! 👏
- Follow us X | LinkedIn | YouTube | Discord
- Visit our other platforms: In Plain English | CoFeed | Venture | Cubed
- More content at Stackademic.com









