Welcome to the Angular signals tutorial! Signals are Angular's reactive primitive that provide a way to manage state and automatically update your UI when that state changes.
In this activity, you'll learn how to:
- Create your first signal using the
signal()function - Display its value in a template
- Update the signal value using
set()andupdate()methods
Let's build an interactive user status system with signals!
-
Import the signal function
Import the
signalfunction from@angular/coreat the top of your component file.import {Component, signal, ChangeDetectionStrategy} from '@angular/core'; -
Create a signal in your component
Add a
userStatussignal to your component class that is initialized with a value of'offline'. -
Display the signal value in the template
Update the status indicator to display the current user status by:
- Binding the signal to the class attribute with
[class]="userStatus()" - Displaying the status text by replacing
???with{{ userStatus() }}
<!-- Update from: --> <div class="status-indicator offline"> <span class="status-dot"></span> Status: ??? </div> <!-- To: --> <div class="status-indicator" [class]="userStatus()"> <span class="status-dot"></span> Status: {{ userStatus() }} </div>Notice how we call the signal
userStatus()with parentheses to read its value. - Binding the signal to the class attribute with
-
Add methods to update the signal
Add methods to your component that change the user status using the
set()method.goOnline() { this.userStatus.set('online'); } goOffline() { this.userStatus.set('offline'); }The
set()method replaces the signal's value entirely with a new value. -
Wire up the control buttons
The buttons are already in the template. Now connect them to your methods by adding:
- Click handlers with
(click) - Disabled states with
[disabled]when already in that status
<!-- Add bindings to the existing buttons: --> <button (click)="goOnline()" [disabled]="userStatus() === 'online'">Go Online</button> <button (click)="goOffline()" [disabled]="userStatus() === 'offline'">Go Offline</button> - Click handlers with
-
Add a toggle method using update()
Add a
toggleStatus()method that switches between online and offline using theupdate()method.toggleStatus() { this.userStatus.update(current => current === 'online' ? 'offline' : 'online'); }The
update()method takes a function that receives the current value and returns the new value. This is useful when you need to modify the existing value based on its current state. -
Add the toggle button handler
The toggle button is already in the template. Connect it to your
toggleStatus()method:<button (click)="toggleStatus()" class="toggle-btn">Toggle Status</button>
Congratulations! You've created your first signal and learned how to update it using both set() and update() methods. The signal() function creates a reactive value that Angular tracks, and when you update it, your UI automatically reflects the changes.
Next, you'll learn how to derive state from signals using computed!