Sub topic 5
Read field state as signals
This chapter assumes you can write a component with signal() and computed(), and that you have built a form with form() and the [field] binding. Signal Forms is experimental in Angular 21, so names may change in later releases.
Two calls: the tree and the state
A form created with form() is a tree that mirrors your model. Each node has two layers:
- Property access moves down the tree:
loginForm.emailis the node for theemailfield. - Calling a node returns its state object:
loginForm.email()is the state of that field.
The root works the same way. loginForm() is the state of the whole form. Every property on a state object is a signal, so you read it by calling it again.
const emailState = loginForm.email();
emailState.value(); // current text
emailState.valid(); // true when no validator failsThe common state signals
| Signal | Meaning |
|---|---|
| `value()` | Current value of the field. On a parent node, the current object or array. |
| `valid()` / `invalid()` | Whether validators pass. A parent is invalid if any child is invalid. |
| `touched()` | True after the user has focused and left the field. |
| `dirty()` | True after the user has changed the value. |
| `errors()` | Array of error objects, each with a `kind` and usually a `message`. |
| `disabled()` | True when a rule has disabled the field. |
touched and dirty are separate. A user can tab through a field without typing, which makes it touched but not dirty. Use touched() to decide when to show errors. Use dirty() to decide whether there are unsaved changes.
Use state in templates and computed signals
Because these are ordinary signals, they work anywhere signals work. The template re-renders when they change, and computed() tracks them as dependencies.
import { Component, computed, signal } from '@angular/core';
import { form, Field, required, email } from '@angular/forms/signals';
@Component({
selector: 'app-state-demo',
imports: [Field],
template: `
<input type="email" [field]="f.email" />
<p>Value: {{ f.email().value() }}</p>
<p>Touched: {{ f.email().touched() }}, dirty: {{ f.email().dirty() }}</p>
<p>Status: {{ status() }}</p>
`,
})
export class StateDemoComponent {
private readonly model = signal({ email: '' });
protected readonly f = form(this.model, (path) => {
required(path.email);
email(path.email);
});
protected readonly status = computed(() =>
this.f.email().valid()
? 'OK'
: `${this.f.email().errors().length} problem(s)`
);
}Type in the input and watch the text update on every keystroke. dirty flips to true on the first change. touched flips to true when the input loses focus.
Common errors
- Forgetting the second call.
f.email.valid()fails becausef.emailis a tree node, not a state object. Writef.email().valid(). - Reading state once and keeping it.
const ok = f.email().valid();captures a plain boolean that never updates. Keep the call inside a template,computed(), oreffect(). - Showing errors too early. An empty required field is invalid before the user does anything. Check
touched()first, as the login example does. - Treating
value()as the only source. The model signal andf.email().value()hold the same data. Prefer the model for logic outside the form, and the state object for template display.
Exercise
A field shows its error message as soon as the page loads, before the user has typed anything. Which state signal should the template check, and how do you write the condition for f.email?
Show answer
Check touched() together with invalid(): @if (f.email().touched() && f.email().invalid()) { ... }. The error then appears only after the user has focused and left the field.