Demo
HTML
TS
CSS
💡 Signal Example 1: Counter
⚡ Experience the power of Angular Signals with this interactive counter! Effortlessly increment or decrement the value and see instant, reactive UI updates—no manual subscriptions or change detection required.
🔢 Signal Counter
1
Current value: 1
The counter uses Angular's signal for reactive state updates.
âš¡ Understanding Angular Signals
Signals are Angular’s new way to manage reactive state. They let you create values that automatically update the UI when changed, without manual subscriptions or observables. This makes state management more predictable and efficient.
A signal is a special function that holds a value. When you read the signal (by calling it as a function), you get its current value. When you update the signal, Angular automatically tracks where it is used and updates only those parts of the UI that depend on it.
How to Use Signals
- Create a signal:
count = signal(0);
This declares a reactive value with an initial value of0. - Read the value:
1
Use in your template orthis.count()in TypeScript to get the current value. - Update the value:
this.count.set(newValue)orthis.count.update(c => c + 1)
Set a new value or update based on the previous value.
How This Example Works
- The counter value is stored in a signal called
count. This makes the value reactive. - When you click + or -, the signal is updated using
updatemethods. The UI updates instantly wherevercount()is used. - Signals automatically track dependencies. If you use
count()in multiple places, all will update when the value changes. - No manual change detection, subscriptions, or RxJS is needed. Signals simplify reactivity and state management in Angular.
- Signals are ideal for local component state, but can also be composed for more complex scenarios.
★Signals make Angular apps more predictable, efficient, and easy to maintain by providing fine-grained reactivity out of the box.
