Sub topic 1
Signal Forms in Angular: Building Forms from Signal Models
Prerequisites
This chapter assumes TypeScript fundamentals, standalone Angular components, Angular templates, and familiarity with signal(). Reactive Forms knowledge helps, but it is not required.
Why Signal Forms and how it differs from Reactive Forms
Signal Forms is Angular’s signal-based forms API, imported from @angular/forms/signals. It was introduced as experimental in Angular 21, so its names and details may change.
Reactive Forms represent controls with objects such as FormGroup and FormControl. Template-driven forms infer that structure from directives in the template. Signal Forms starts with a writable signal containing the form’s data. Angular derives field state and validation from that model.
| Approach | Main source of truth | Form structure |
|---|---|---|
| Template-driven | Template and directives | Inferred |
| Reactive Forms | Control tree | Explicit control objects |
| Signal Forms | Writable signal model | Derived with `form()` |
This model keeps application data and form data closely aligned. A field value is read or changed through the signal model, while validity, touched state, and errors are exposed as signals.
Start with a signal model
Define the shape of the data first. The signal is the source of truth and can later be sent to an API.
Create a form with form()
Pass the model to form(). Its schema function declares rules against paths such as path.email.
Bind fields to inputs
The [field] directive connects an input to a field. The field’s state is callable because it is signal-based.
import { Component, signal } from '@angular/core';
import { email, Field, form, required } from '@angular/forms/signals';
@Component({
selector: 'app-login',
imports: [Field],
template: `
<form>
<label>Email
<input type="email" [field]="loginForm.email">
</label>
@if (loginForm.email().invalid()) {
<p>{{ loginForm.email().errors()[0]?.message }}</p>
}
<label>Password
<input type="password" [field]="loginForm.password">
</label>
<button [disabled]="loginForm().invalid()">Log in</button>
</form>
`,
})
export class LoginComponent {
protected readonly model = signal({ email: '', password: '' });
protected readonly loginForm = form(this.model, (path) => {
required(path.email);
email(path.email);
required(path.password);
});
}Exercise
What is the source of truth in this example, and what does form() add?
Show answer
model is the source of truth. form() derives field objects, state, and validation from it.