adev/src/content/tutorials/signals/steps/1-creating-your-first-signal/README.md
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:
signal() functionset() and update() methodsLet's build an interactive user status system with signals!
<hr /> <docs-workflow> <docs-step title="Import the signal function"> Import the `signal` function from `@angular/core` at the top of your component file.import {Component, signal, ChangeDetectionStrategy} from '@angular/core';
@Component({
/* Config omitted */
})
export class App {
userStatus = signal<'online' | 'offline'>('offline');
}
<!-- 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.
</docs-step>
goOnline() {
this.userStatus.set('online');
}
goOffline() {
this.userStatus.set('offline');
}
The set() method replaces the signal's value entirely with a new value.
<!-- Add bindings to the existing buttons: -->
<button (click)="goOnline()" [disabled]="userStatus() === 'online'">Go Online</button>
<button (click)="goOffline()" [disabled]="userStatus() === 'offline'">Go Offline</button>
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.
<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!