Introduction
Angular has introduced Signals as a modern reactive programming feature that simplifies state management and improves application performance. While Angular Forms have traditionally relied on Template-driven Forms and Reactive Forms using RxJS Observables, Signals provide a new way to manage form state with less boilerplate and more predictable data flow.
What are Angular Signals?
Signals are a reactive state management feature that automatically updates the UI when their values change.
Example:
import { signal } from '@angular/core';
name = signal('John');
console.log(name());
name.set('Tom');
console.log(name());
Output:
Tom
Explanation:
- signal() creates a reactive value.
- set() updates the value.
- Calling the signal like a function (name()) retrieves its current value.
Note: Whenever the value changes, Angular automatically updates the UI.
Why Use Signals in Forms?
There are many advantages to using Signals in Forms
1. Simpler state management.
2. For automatic UI updates.
3. For Less boilerplate code.
4. For Better performance.
Traditional Reactive Forms vs Signals
| Feature | Reactive Forms | Signals |
|---|---|---|
| State Management | Uses RxJS Observables | Uses reactive Signals |
| Subscriptions | Requires subscriptions | No subscriptions required |
| Code Complexity | More boilerplate | Cleaner code |
| Best Use Case | Good for complex async operations | Great for local UI state |
| Dependency Management | Manual unsubscribe may be required | Automatic dependency tracking |
Creating a Simple Signal
Creating a Signal is straightforward.
import { signal } from '@angular/core';
email = signal('');
Updating the value:
email.set('admin@example.com');
Reading the value:
console.log(email());
Output:
Note: Angular automatically refreshes the UI wherever this signal is used.
Use Signals with Angular Form
In Component file:
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-form',
templateUrl: './form.component.html'
})
export class FormComponent {
name = signal('');
email = signal('');
submitForm() {
console.log(this.name(), this.email());
}
}
In the template file:
<input
type="text"
placeholder="Enter Name"
(input)="name.set($event.target.value)"
>
<input
type="email"
placeholder="Enter Email"
(input)="email.set($event.target.value)"
>
<button (click)="submitForm()">Submit</button>
Note: When users type in the input fields, the signals update automatically.
Using Computed Signals
Angular also provides computed(), which derives values automatically. It always returns the latest value.
import { signal, computed } from '@angular/core';
firstName = signal('John');
lastName = signal('Taylor');
fullName = computed(() =>
`${firstName()} ${lastName()}`
);
fullName();
Output:
Note: If either signal changes, Angular recalculates automatically.
Benefits of Using Signals in Angular Forms
1. Less Boilerplate
- No subscriptions.
- No Subjects.
- No BehaviorSubjects.
- Code becomes significantly shorter.
2. Better Performance
Angular updates only the components that depend on the changed signal. This fine-grained reactivity minimizes unnecessary rendering and improves application responsiveness.
3. Automatic Dependency Tracking
Angular automatically knows which parts of the template depend on which signals. Developers don’t need to manually wire change detection.
4. Improved Readability
Signals use straightforward syntax.
count()
count.set(10)
This is easier to understand than managing streams with Observables in simple scenarios.
5. Easier Maintenance
Less reactive boilerplate means fewer opportunities for bugs and easier long-term maintenance.
Limitations of Signals
Although Signals are powerful, they are not the best solution for every problem.
There are some limitations
- Not a replacement for RxJS
- Less suitable for complex asynchronous workflows
- HTTP requests still commonly use Observables
- WebSocket streams continue to benefit from RxJS
- Reactive Forms remain the preferred solution for advanced validation scenarios
When to Use Signals in Angular Forms
There are many reasons to use signals in Angular Forms
1. You can use signals when building simple forms.
2. You can use signals when managing local form state.
3. You can use signals when creating small UI interactions.
Conclusion
Signals provide a modern and lightweight way to manage form state in Angular.
Reactive Forms is better for complex forms with advanced validation.