Angular Signals Explained: signal(), computed() and effect() With Examples
On this page
- The three building blocks
- A shopping cart with signals
- Signal inputs and model
- Signals and RxJS together
- When to use effect(), and when not to
- Signals compared with React state
- How signals know what to update
- A shared state service with signals
- Loading data from an API
- Migrating an existing component step by step
- Common mistakes with signals
- Frequently asked questions
- Oh hi there 👋It’s nice to meet you.
- Sign up to receive awesome content in your inbox.
Signals are Angular’s modern way to handle changing state. A signal holds a value and notifies Angular whenever that value changes, so Angular knows exactly which parts of the page to update. They make components simpler than juggling RxJS subjects for local state, and they are the foundation for Angular’s move away from Zone.js.
The three building blocks
| Function | What it does | Example |
|---|---|---|
signal() |
Holds a value you can change | count = signal(0) |
computed() |
A read-only value derived from other signals | double = computed(() => this.count() * 2) |
effect() |
Runs code whenever the signals it reads change | Saving to localStorage, logging |
A shopping cart with signals
import { Component, computed, effect, signal } from '@angular/core';
interface Item { id: number; name: string; price: number; qty: number; }
@Component({
selector: 'app-cart',
template: `
@for (item of items(); track item.id) {
<p>
{{ item.name }} × {{ item.qty }}
<button (click)="add(item.id)">+</button>
</p>
}
<p>Items: {{ count() }}, total: ₹{{ total() }}</p>
`,
})
export class CartComponent {
items = signal<Item[]>([
{ id: 1, name: 'Keyboard', price: 1499, qty: 1 },
{ id: 2, name: 'Mouse', price: 699, qty: 2 },
]);
count = computed(() => this.items().reduce((n, i) => n + i.qty, 0));
total = computed(() => this.items().reduce((sum, i) => sum + i.price * i.qty, 0));
constructor() {
effect(() => localStorage.setItem('cart', JSON.stringify(this.items())));
}
add(id: number) {
this.items.update((list) => list.map((i) => (i.id === id ? { ...i, qty: i.qty + 1 } : i)));
}
}
Four details to notice:
- You read a signal by calling it:
items(), both in TypeScript and in the template. set(value)replaces the value;update(fn)computes the new value from the old one.computed()values recalculate only when a signal they read changes, and they are cached in between.- Update arrays and objects immutably (create a new array), so Angular sees that the value changed.
Signal inputs and model
Component inputs can be signals too, which means you can use them in computed() directly:
import { Component, computed, input, model } from '@angular/core';
@Component({
selector: 'app-price',
template: `<span>₹{{ withTax() }}</span>`,
})
export class PriceComponent {
amount = input.required<number>(); // read-only input from the parent
taxRate = input(0.18); // input with a default value
withTax = computed(() => Math.round(this.amount() * (1 + this.taxRate())));
}
@Component({
selector: 'app-rating',
template: `<button (click)="value.set(value() + 1)">★ {{ value() }}</button>`,
})
export class RatingComponent {
value = model(0); // two-way binding: <app-rating [(value)]="stars" />
}
Signals and RxJS together
Signals don’t replace RxJS. Observables are still the better tool for streams of events over time: HTTP requests, websockets, debounced search. Angular provides bridges in @angular/core/rxjs-interop:
import { toSignal } from '@angular/core/rxjs-interop';
import { HttpClient } from '@angular/common/http';
import { inject } from '@angular/core';
export class ProductsComponent {
private http = inject(HttpClient);
products = toSignal(this.http.get<Product[]>('/api/products'), { initialValue: [] });
}
toSignal subscribes for you and unsubscribes when the component is destroyed. toObservable goes the other way.
When to use effect(), and when not to
Use effect() for side effects that leave Angular’s world: saving to storage, logging, calling a non-Angular library. Don’t use it to copy one signal into another; that is what computed() is for. Writing signals inside effects easily creates loops and hard-to-follow code.
Signals compared with React state
| Angular signals | React useState | |
|---|---|---|
| Read a value | count() |
count |
| Change it | count.set(1) or update() |
setCount(1) |
| Derived values | computed(), cached automatically |
Calculated each render, or useMemo |
| What re-runs on change | Only the code and template parts that read the signal | The whole component function |
How signals know what to update
The magic behind signals is dependency tracking. Whenever code reads a signal inside a reactive context, such as a template, a computed() or an effect(), Angular records that this context depends on that signal. When the signal changes, Angular knows exactly which computed values are now stale and which parts of which templates need to be checked again. Everything else is left alone.
Before signals, Angular relied on Zone.js, which patched browser APIs such as timers, events and HTTP calls to detect when “something might have changed”, then checked component trees for differences. That works, but it often checks far more than needed. With signals, Angular is told precisely what changed. This is why the framework is moving towards zoneless applications, where change detection is driven by signals and events rather than by Zone.js watching everything.
Computed signals are also lazy and memoized: a computed() doesn’t recalculate when its dependencies change, only the next time someone reads it, and if nothing it depends on has changed, it returns the cached value. Expensive derived values, such as filtering a long list, become cheap to read many times.
A shared state service with signals
For state shared between components, such as the logged-in user or a shopping cart, put signals in a service. Keep the writable signal private and expose a read-only version, so only the service can change it:
import { Injectable, computed, signal } from '@angular/core';
export interface CartItem { id: number; name: string; price: number; qty: number; }
@Injectable({ providedIn: 'root' })
export class CartService {
private readonly items = signal<CartItem[]>([]);
readonly list = this.items.asReadonly();
readonly count = computed(() => this.items().reduce((n, i) => n + i.qty, 0));
readonly total = computed(() => this.items().reduce((s, i) => s + i.price * i.qty, 0));
add(product: Omit<CartItem, 'qty'>) {
this.items.update((list) => {
const found = list.find((i) => i.id === product.id);
return found
? list.map((i) => (i.id === product.id ? { ...i, qty: i.qty + 1 } : i))
: [...list, { ...product, qty: 1 }];
});
}
remove(id: number) {
this.items.update((list) => list.filter((i) => i.id !== id));
}
}
// any component
export class HeaderComponent {
cart = inject(CartService);
// template: <span>Cart ({{ cart.count() }})</span>
}
For many apps this replaces a full state management library. Every component that reads cart.count() updates automatically when an item is added anywhere in the app.
Loading data from an API
Real apps load data from a server, often an Express API in a MEAN stack. A common pattern combines a signal for the filter with an HTTP request that reacts to it:
import { Component, inject, signal } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { toObservable, toSignal } from '@angular/core/rxjs-interop';
import { debounceTime, switchMap } from 'rxjs';
@Component({
selector: 'app-product-search',
template: `
<input [value]="query()" (input)="query.set($any($event.target).value)" placeholder="Search products">
@for (p of results(); track p._id) { <p>{{ p.title }}</p> } @empty { <p>No products found.</p> }
`,
})
export class ProductSearchComponent {
private http = inject(HttpClient);
query = signal('');
results = toSignal(
toObservable(this.query).pipe(
debounceTime(300),
switchMap((q) => this.http.get<{ _id: string; title: string }[]>('/api/products', { params: { q } }))
),
{ initialValue: [] }
);
}
This shows the division of labour clearly: the signal holds the current query, RxJS handles timing (waiting 300 ms after typing stops, and cancelling outdated requests with switchMap), and toSignal turns the result back into a signal the template can read. Newer Angular versions also offer a resource() API that loads async data from signals directly; check the official documentation for its current status in your version.
Migrating an existing component step by step
You don’t need to rewrite an app to use signals. Convert one component at a time:
- Replace simple fields with signals.
count = 0becomescount = signal(0), and{{ count }}in the template becomes{{ count() }}. - Replace getters that derive values with
computed(). They now cache their result instead of recalculating on every check. - Replace
@Input()decorators withinput()and@Output()withoutput(). - Replace
BehaviorSubjectstate in services with signals where the state is mainly displayed, keeping RxJS where you need operators over time. - Remove manual change detection calls such as
markForCheck()that the signals now make unnecessary.
Components using signals work fine next to components that don’t, so the migration can happen gradually over many releases.
Common mistakes with signals
- Forgetting the parentheses in templates.
{{ count }}prints the signal function itself, not its value. Always write{{ count() }}. - Mutating objects inside a signal.
user().name = 'Aarav'changes the object without telling Angular. Useuser.update(u => ({ ...u, name: 'Aarav' })). - Creating effects outside an injection context.
effect()must be created in a constructor, a field initializer or with an explicit injector; creating one inside a click handler throws an error. - Using effects for derived state. If you find yourself setting one signal from another in an effect, a
computed()is almost always the better tool.
Frequently asked questions
Do signals replace RxJS in Angular?
No. Use signals for state that templates display, and RxJS for asynchronous streams such as HTTP calls and user events. Convert between them with toSignal and toObservable.
Why do I have to call a signal like a function?
Calling it lets Angular record which code reads which signal. That tracking is what allows Angular to update only the affected parts of the page.
Can I mutate an array inside a signal?
Avoid it. Create a new array with update() so Angular detects the change. Mutating the old array in place may not update the view.
Are signals only for Angular?
No. The idea is shared by several frameworks, including SolidJS, Preact, Vue’s reactivity system and Svelte’s runes, and there is a proposal to add signals to JavaScript itself. Learning them in Angular helps you understand all of these.




