signal()
Creates a writable reactive value that notifies consumers when it changes.
Syntax
signal<T>(initialValue: T, options?): WritableSignal<T> Parameters
| Name | Type | Required | Description |
|---|---|---|---|
initialValue | T | No | The initial value held by the signal. |
options | object | No | Optional config; supports an equal comparator function. |
Returns
WritableSignal<T> — A getter function with set(), update(), and asReadonly() methods.
Examples
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-counter',
standalone: true,
template: `<button (click)="inc()">{{ count() }}</button>`,
})
export class CounterComponent {
count = signal(0);
inc() {
this.count.update((n) => n + 1);
}
}
const user = signal({ name: 'Ada' });
user.set({ name: 'Grace' });
console.log(user().name);
Notes
Read a signal by calling it as a function: `count()`. Use `set()` to replace
the value and `update()` to derive the next value from the current one.
Reading inside a template or computed/effect creates a reactive dependency.
Use the `equal` option to skip notifications for values you consider equal.