Angular Signals
Angular Signals is a powerful system that provides detailed monitoring of state usage within an application, enabling the framework to efficiently optimize rendering updates.
import { Component, OnInit } from '@angular/core';
import { signal, computed } from '@angular/core'; // Import from '@angular/core'
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
count = signal(0);
doubleCount = computed(() => this.count() * 2);
constructor() {}
ngOnInit() {
// Optional logging for debugging displayedCount changes
// console.log('Displayed count changed to:', this.displayedCount());
}
incrementCount() {
this.count.set(this.count() + 1);
}
decrementCount() {
this.count.update((value) => Math.max(0, value - 1));
}
}
<h1>Angular Signals Example</h1>
<button (click)="incrementCount()" style="margin-right: 10px;">Increment Count</button>
<button (click)="decrementCount()">Decrement Count</button>
<p>Count: {{ count() }}</p>
<p>Double Count: {{ doubleCount() }}</p>