Sub topic 4
Bind fields to inputs
This chapter shows how the [field] directive connects a field created by form() to an HTML control. It assumes you know TypeScript, standalone components, and signals (signal(), computed(), effect()). It also assumes you have read the earlier chapters on the signal model and form(). Signal Forms is experimental in Angular 21, so the names used here (Field and [field]) may change in later releases.
What binding does
form(model) gives you a tree of fields, but nothing is connected to the page yet. The [field] directive links one field to one control. After that, three things stay in sync without extra code:
- The control shows the field's current value.
- User input writes back to the field, and from there to the model signal.
- The field's state (disabled, touched, and so on) is applied to the control.
The model stays the source of truth. A field is a view onto one part of that model, so writing to the model from code also updates the input.
A complete example
The component below binds a text input, a number input, a checkbox, a select, and a textarea. Each uses the same [field] syntax.
import { Component, signal } from '@angular/core';
import { form, Field } from '@angular/forms/signals';
@Component({
selector: 'app-profile',
imports: [Field],
template: `
<form (submit)="onSubmit($event)">
<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" />
Send me the newsletter
</label>
<label>
Country
<select [field]="profileForm.country">
<option value="de">Germany</option>
<option value="us">United States</option>
<option value="jp">Japan</option>
</select>
</label>
<label>Bio <textarea [field]="profileForm.bio"></textarea></label>
<p>Hello, {{ model().name || 'stranger' }}!</p>
<button type="submit">Save</button>
</form>
`,
})
export class ProfileComponent {
protected readonly model = signal({
name: '',
age: 18,
newsletter: false,
country: 'de',
bio: '',
});
protected readonly profileForm = form(this.model);
protected onSubmit(event: Event) {
event.preventDefault();
console.log(this.model());
}
}The paragraph that greets the user reads model().name. It updates on every keystroke because the directive writes each edit into the model signal.
Importing the directive
The directive's class is Field. In a standalone component you add it to imports. If you forget, Angular treats [field] as an unknown property of the element, and the build or the console reports an error. Because Field is an ordinary directive, it also works in any component whose template contains bound controls. It is not needed in components that only read state.
Binding a field, not a value
The expression in [field]="..." is the field itself. It is not a call and not a value.
<!-- Correct: the field -->
<input [field]="profileForm.name" />
<!-- Wrong: this passes the field's state object, not the field -->
<input [field]="profileForm.name()" />
<!-- Wrong: this is only a one-way string, so nothing is connected -->
<input [value]="profileForm.name().value()" />This distinction matters throughout Signal Forms. Without parentheses you have the field, which is what binds. With parentheses you have its state, which is what you read. Use profileForm.name().value() when you want the current text, and profileForm.name when you want to bind.
Supported controls
The directive understands the standard form controls and chooses how to read and write based on the element:
| Element | Field value type | Notes |
|---|---|---|
| `<input type="text">` and similar text types | `string` | Updates as the user types |
| `<input type="number">` | `number` | The directive converts the text to a number for you |
| `<input type="checkbox">` | `boolean` | Reads and writes the checked state |
| `<input type="radio">` | `string` | Give each radio the same field and a distinct `value` |
| `<select>` | `string` | Matches the option `value` attributes |
| `<textarea>` | `string` | Same as a text input |
Since the model has a type, a mismatch shows up at compile time. A checkbox bound to a string field, for example, is a type problem you can fix before running anything.
Radio buttons share one field:
<label><input type="radio" value="free" [field]="planForm.plan" /> Free</label>
<label><input type="radio" value="pro" [field]="planForm.plan" /> Pro</label>The selected radio is the one whose value equals the field's value.
Nested objects
The field tree mirrors the shape of the model. For a nested model, you bind by following the same path.
import { Component, signal } from '@angular/core';
import { form, Field } from '@angular/forms/signals';
@Component({
selector: 'app-address',
imports: [Field],
template: `
<label>Street <input [field]="addressForm.address.street" /></label>
<label>City <input [field]="addressForm.address.city" /></label>
<p>{{ addressForm.address.city().value() }}</p>
`,
})
export class AddressComponent {
protected readonly addressForm = form(
signal({ address: { street: '', city: '' } }),
);
}Here addressForm.address is itself a field, and .street and .city are its children. Each child is bound independently.
What the directive applies for you
The directive does more than copy values. It also reflects field state onto the element:
- Touched. When the control loses focus, the field is marked as touched. This is what lets you show an error only after the user has visited the input.
- Disabled. If the field is disabled by a rule in the schema, the directive disables the element.
- Read-only and similar attributes. Rules that make a field read-only or required are reflected on the element where that makes sense.
Because of this, avoid setting these attributes yourself on a bound control.
Changing values from code
Because the model is a signal, you can update it directly, or you can update through a field.
// Through the model
this.model.update((m) => ({ ...m, name: 'Ada' }));
// Through a field's value signal
this.profileForm.name().value.set('Ada');Both update the input on screen. The second form is usually clearer when you are changing a single field, since you do not have to rebuild the whole object. Resetting a form to defaults is a model update: call this.model.set({ ... }) with a fresh object.
Common errors
- Binding the state instead of the field.
[field]="f.name()"fails type checking or does nothing. Remove the parentheses. - Missing import. If
[field]is not recognized, addFieldto the component'simportsarray. - Wrong value type. A number input bound to a string field, or the reverse, shows a type error. Fix the model's initial values so each has the type the control produces.
- Starting with
undefined. A field whose initial value isundefinedhas no useful type for the control. Start text fields with'', checkboxes withfalse, and numbers with a number. - Mixing in
ngModelorformControl. Signal Forms does not use those directives. Put one binding mechanism on each control. - Reusing a field on unrelated controls. One field should drive one control, apart from radio groups where the sharing is intended.
Exercise
A teammate writes <input type="text" [field]="signupForm.email()" /> and the input never reflects typed text in the model. What is wrong, and how do you fix it? Also, how do you read the current email text in the component class?
Show answer
The binding passes the field's state object (the result of calling the field) instead of the field. Remove the parentheses: [field]="signupForm.email". To read the current text in the class, call the field to get its state and then call value: this.signupForm.email().value().
Exercise
Your model is signal({ plan: 'free', agree: false }). Write the two template lines that bind a radio group for plan (options free and pro) and a checkbox for agree, assuming the form is stored in checkoutForm.
Show answer
Radios share one field and use distinct value attributes: <input type="radio" value="free" [field]="checkoutForm.plan" /> and <input type="radio" value="pro" [field]="checkoutForm.plan" />. The checkbox is <input type="checkbox" [field]="checkoutForm.agree" />.