AI Hints
Home›Angular Tutorial›What Signals Are, signal() and set() and update()
Angular · Lesson 28 of 120

What Signals Are

Values that notify.

⏱ 2 min read▶ Video at 34:08🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Data in an app changes all the time. Stock goes down, baskets fill up, and prices change. Angular needs to know when, so it can update the page. That's what signals are for.

  • Data changes
  • Angular must know
  • Signals tell it

A value that notifies

A signal is a box that holds a value. You read the value by calling the signal, with brackets. You change it with set, or update. And whenever it changes, it tells everyone who read it.

keyvalue
A signala box holding a value
Readstock()
Changeset(), update()
On changeit notifies its readers

How signals update the page

The template reads a signal, so Angular remembers that spot. Later, some code sets a new value. The signal notifies Angular. And Angular updates just that spot, nothing else.

  1. The template reads stock()
  2. Angular remembers the spot
  3. Code sets a new value
  4. The signal notifies
  5. Only that spot updates

A first signal

Stock is a signal, starting at five. The template reads it, by calling it with brackets. Sell sets the stock to its current value, minus one. We click sell twice. Run it. The stock went down to three, and the page followed.

app.tstypescript
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <h2>Dune</h2>
    <p>Copies in stock: {{ stock() }}</p>
    <button (click)="sell()">Sell one</button>
  `,
})
export class App {
  stock = signal(5);

  sell() { this.stock.set(this.stock() - 1); }
}
Also uses actions.json
[{"click": "button", "times": 2}]

Summary

A signal holds a value, notifies when it changes, and Angular updates the page for us. Next, creating, and reading signals.

Key takeaways
  • A box with a value
  • Read with ()
  • Notifies on change
Next up: signal() — create and read