October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Create and Update Your First Signal in Angular

A code-first introduction to Angular's writable signals: initialize with signal(), read by calling the signal, and update with .set() or .update().
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use signal() to create writable state in an Angular component, call the signal to read its current value, and change it with .set() or .update(). The example below displays a user status and lets a reader set or toggle it.

Create a writable signal

Import signal from @angular/core and initialize it in your component. This example limits the status to two valid strings:

As an Amazon Associate I earn from qualifying purchases.

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

@Component({
  selector: 'app-root',
  template: `
    

Status: {{ userStatus() }}

`, }) export class App { userStatus = signal<'online' | 'offline'>('offline'); goOnline() { this.userStatus.set('online'); } toggleStatus() { this.userStatus.update(current => current === 'online' ? 'offline' : 'online'); } }

signal<'online' | 'offline'>('offline') creates a writable signal whose initial value is 'offline'. The type annotation restricts later values to 'online' or 'offline'. Angular’s tutorial describes signals as a reactive primitive for managing state and automatically updating the UI when that state changes.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read the signal in the template

Call the signal to get its current value: {{ userStatus() }}. A signal is callable; using userStatus without parentheses refers to the signal itself, not the value you want to display. Angular tracks signal reads in templates. When a signal read in an OnPush component template changes, Angular marks that component for an update during change detection.

Choose between .set() and .update()

Both methods write to the same writable signal. Choose based on how you know the next value:

Method Use it when Example
.set(value) The replacement value is already known. this.userStatus.set('online')
.update(fn) The next value should be calculated from the current value. this.userStatus.update(current => current === 'online' ? 'offline' : 'online')

Replace the value with .set()

The goOnline() method supplies a specific value, so it uses .set('online'). In the template, (click)="goOnline()" connects the button click to that component method.

Derive the next value with .update()

The toggle method needs to inspect the current status before choosing the next one. Its callback receives the current value as current and returns the replacement value. The template’s (click)="toggleStatus()" binding calls that method when the button is clicked.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep writable and derived state distinct

signal() creates writable state that you can change with .set() or .update(). By contrast, Angular’s computed() creates read-only derived state; you do not change a computed signal directly with those write methods. See Angular’s signals essentials and signals guide for more on reads, writes, computed values, and template consumers.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.