Sub topic 4
Bind fields to inputs
This chapter assumes you have read the previous chapters. You should know how to build a signal model and call form() on it. It also assumes you know basic HTML input types.
The binding connects the field tree to the DOM
form() returns a tree of fields. A field does nothing visible until you attach it to an element. The [field] directive does that. It is provided by the Field class from @angular/forms/signals, and you add it to the component's imports array.
Once bound, the directive does two jobs:
- It writes the field's value into the input.
- It writes the user's edits back into the model signal.
The directive also reports focus and blur events, so the field's touched state updates.
Bind different input types
You bind the field itself, not its state. Write profileForm.name, not profileForm.name(). Calling the field gives you the state object, which you use for reading (see the next chapter).
import { Component, signal } from '@angular/core';
import { form, Field } from '@angular/forms/signals';
@Component({
selector: 'app-profile',
imports: [Field],
template: `
<form>
<label>Name <input type="text" [field]="profileForm.name" /></label>
<label>Age <input type="number" [field]="profileForm.age" /></label>
<label>
<input type="checkbox" [field]="profileForm.newsletter" /> Newsletter
</label>
<label>
Country
<select [field]="profileForm.country">
<option value="de">Germany</option>
<option value="fr">France</option>
</select>
</label>
<label>Bio <textarea [field]="profileForm.bio"></textarea></label>
</form>
<p>Model: {{ model() | json }}</p>
`,
})
export class ProfileComponent {
protected readonly model = signal({
name: '',
age: 0,
newsletter: false,
country: 'de',
bio: '',
});
protected readonly profileForm = form(this.model);
}Because the template uses the json pipe, add JsonPipe from @angular/common to imports before you run this. Type in the name input and the model text changes immediately. No (input) handler or ngModel is involved.
The model's property types decide what the control holds. A boolean property fits a checkbox, a number fits a number input, and a string fits text, select, and textarea.
Common errors
| Symptom | Likely cause |
|---|---|
| The input stays empty and the console reports an unknown property `field` | `Field` is missing from the component's `imports` |
| The binding throws or does nothing | You passed `form.name()` (the state) instead of `form.name` (the field) |
| The input resets or ignores typing | A second binding such as `[value]` or `(input)` on the same element competes with the directive |
| A number input writes a string into the model | The model property was initialized as `''` instead of a number |
Update the model from code
The binding runs in both directions. If you call this.model.set(...) or this.model.update(...), the bound inputs show the new values. This is useful for resetting a form or loading saved data. The model stays the single source of truth.
Exercise
A teammate writes <input [field]="loginForm.email()" /> and the input does not work. What is wrong, and what is the fix?
Show answer
The binding receives the field's state object, because calling loginForm.email() returns it. The directive needs the field itself. Write [field]="loginForm.email".
Exercise
You change the model with model.set({ ...model(), name: 'Ada' }). Do the bound inputs update, and why?
Show answer
Yes. The directive reads the field value, which is derived from the model signal, so the input shows 'Ada' without any extra code.