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

signal()

Create and read.

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

Let's look closer at creating signals, and reading them, in the template, and in the class.

  • Create a signal
  • Read it in the template
  • Read it in the class

Creating signals

Signal, with a starting value, creates one. TypeScript works out the type from that value. You can also give the type in angle brackets, like signal of number. And it's usually a property of the component class.

keyvalue
signal('The Bookshop')a string signal
signal(true)a boolean signal
signal<number>(0)type written out
Wherea class property

Reading signals

Here are three signals, a string, a boolean, and a number. The template reads them with brackets. The class reads them the same way, with this dot. We click report. Run it. The page shows the shop, and that it's open. And the report used two signals from the class.

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

@Component({
  selector: 'app-root',
  template: `
    <p>Shop: {{ shopName() }}</p>
    <p>Open: {{ isOpen() ? 'yes' : 'no' }}</p>
    <button (click)="report()">Report</button>
  `,
})
export class App {
  shopName = signal('The Bookshop');
  isOpen = signal(true);
  visitors = signal<number>(0);

  report() {
    console.log(`${this.shopName()} has ${this.visitors()} visitors`);
  }
}
Output
The Bookshop has 0 visitors
Also uses actions.json
[{"click": "button"}]

Brackets or not?

Calling the signal with brackets gives you its value. Without brackets, you get the signal itself, the box. So in templates, and in code, write shop name with brackets, to see the text.

shopName()

  • the value'The Bookshop'use this to show it

shopName

  • the signal itselfthe boxuse this to change it

Summary

Create signals with signal, and a starting value. Read them by calling them, with brackets, everywhere. Next, changing signals.

Key takeaways
  • signal(5)
  • stock()
  • signal<number>(0)
Next up: set() and update() — change a signal