Sub topic 3
Create a form with form()
This chapter covers the form() function, which turns a writable signal into a form. It assumes you know TypeScript, standalone components, and the basics of signal(), computed() and effect(). The previous chapter showed how to design the signal model. Here you will see what form() does with that model and how to read the object it returns.
Signal Forms is experimental in Angular 21. The names in this chapter match that release, and later versions may rename them.
What form() does
form() is the entry point of the API. You import it from @angular/forms/signals and pass it a writable signal. It returns a field tree, an object whose shape mirrors your model. Each node in the tree stands for one piece of the model and holds that piece's state.
import { signal } from '@angular/core';
import { form } from '@angular/forms/signals';
const model = signal({ name: '', age: 0 });
const profileForm = form(model);profileForm does not copy model. It reads from the signal and writes back to it. The form has no separate value store, so the model is the single source of truth.
The signature
The call takes a model, an optional schema function and an optional options object.
form(model);
form(model, (path) => { /* rules */ });
form(model, (path) => { /* rules */ }, { injector });- model is a
WritableSignalholding an object. The object's type gives the form its type. - schema function is optional. It receives a
pathobject that mirrors the model, and it declares validators and conditional rules. It runs once, when the form is created. Later chapters cover the rules you can put in it. - options configures the form. The one you will meet first is
injector, explained below.
A form with no schema is valid. It tracks values and interaction state, and it has no rules.
Where to call form()
form() needs an injection context because it creates effects and other reactive pieces. In a component, a field initializer or the constructor works.
import { Component, signal } from '@angular/core';
import { form } from '@angular/forms/signals';
@Component({
selector: 'app-profile',
template: `<p>{{ model().name }}</p>`,
})
export class ProfileComponent {
protected readonly model = signal({
name: '',
address: { city: '', zip: '' },
});
protected readonly profileForm = form(this.model);
}If you must build a form later, such as inside an event handler, pass an Injector in the options. Otherwise Angular throws a missing-injection-context error.
import { Component, Injector, inject, signal } from '@angular/core';
import { form } from '@angular/forms/signals';
@Component({ selector: 'app-late', template: '' })
export class LateComponent {
private readonly injector = inject(Injector);
createForm() {
const model = signal({ title: '' });
return form(model, undefined, { injector: this.injector });
}
}Passing undefined for the schema keeps the options in the third position.
Reading the field tree
The field tree has two layers of access, and mixing them up is the most common early mistake.
| Expression | What you get |
|---|---|
| `profileForm` | The root of the tree. Use it to reach child fields. |
| `profileForm()` | The state of the whole form. |
| `profileForm.name` | The child field for `name`. Use it in `[field]` bindings. |
| `profileForm.name()` | The state of the `name` field. |
| `profileForm.name().value()` | The current value, read as a signal. |
| `profileForm.address.city` | A nested child field. |
A child field is a property. Its state is what you get by calling it. You pass the child field to the template binding and you call it to read state.
const city = this.profileForm.address.city;
city().value(); // current text
city().value.set('Oslo'); // write a new value
city().touched(); // has the user focused and left it?
this.profileForm().valid(); // is the whole form valid?State properties such as value, valid, touched and dirty are signals, so they work in computed(), effect() and templates. The next chapters cover them in detail. This chapter only needs value.
The model and the form stay in sync
Writing through the form updates the model. Writing to the model updates the form. Both directions work because the form reads the model signal directly.
import { Component, signal } from '@angular/core';
import { form } from '@angular/forms/signals';
@Component({ selector: 'app-sync', template: '' })
export class SyncComponent {
protected readonly model = signal({ name: '', age: 0 });
protected readonly f = form(this.model);
demo() {
this.f.name().value.set('Ada');
console.log(this.model().name); // "Ada"
this.model.update((m) => ({ ...m, name: 'Grace' }));
console.log(this.f.name().value()); // "Grace"
}
}This is useful in practice. To load saved data into a form, set the model. To read the data for submission, read the model. You never call a patchValue() or getRawValue() equivalent.
Replacing the whole model
Replacing the model with model.set({ ... }) updates every field. Use it to reset to initial values or to load a record from the server. Keep the initial value in a function so you can reuse it.
const emptyProfile = () => ({ name: '', age: 0 });
const model = signal(emptyProfile());
function reset() {
model.set(emptyProfile());
}reset() here restores the values only. Interaction state such as touched and dirty is separate from the values. Check the field state API for how to clear it.
How the model shapes the tree
The structure of the field tree comes from the model's runtime value, so the model must be complete from the start.
- Objects become nested field groups.
address.citybecomesform.address.city. - Primitives (string, number, boolean) become leaf fields that bind to inputs.
- Arrays become a list of child fields, one per element. A later chapter covers them.
Because the tree is built from the value, avoid leaving keys out.
// Avoid: the `nickname` field does not exist in the tree.
const bad = signal<{ name: string; nickname?: string }>({ name: '' });
// Prefer: every field has a starting value.
const good = signal({ name: '', nickname: '' });Use '' for text, 0 for numbers, false for booleans, and null where "no value" is meaningful, such as an unselected date. Type a null default explicitly so TypeScript allows other values later:
const model = signal<{ birthday: Date | null }>({ birthday: null });Common mistakes
- Creating the form inside a computed or template. The form would be rebuilt on each evaluation and lose its state. Create it once, in a field initializer or constructor.
- Calling
form()outside an injection context without aninjectoroption. Angular reports a missing injection context. - Forgetting the call parentheses.
profileForm.name.value()fails becauseprofileForm.nameis a field, not its state. WriteprofileForm.name().value(). - Mutating the model object in place. Signals detect changes by reference. Use
set()orupdate()with a new object, or write throughfield().value.set(). - Copying the model into a second variable and editing that copy. The form only sees the signal.
A complete example
This component creates a form with a schema function and shows live values from the model. The required rule uses a built-in validator, which the next chapters explain. It is included only to show where the schema goes.
import { Component, signal } from '@angular/core';
import { form, Field, required } from '@angular/forms/signals';
@Component({
selector: 'app-contact',
imports: [Field],
template: `
<label>Name <input [field]="contactForm.name" /></label>
<label>City <input [field]="contactForm.address.city" /></label>
<p>Name: {{ model().name }}</p>
<p>City: {{ model().address.city }}</p>
<p>Form valid: {{ contactForm().valid() }}</p>
`,
})
export class ContactComponent {
protected readonly model = signal({
name: '',
address: { city: '' },
});
protected readonly contactForm = form(this.model, (path) => {
required(path.name, { message: 'Name is required' });
});
}Typing in either input updates model(), and the paragraphs update without any subscription. The form reports invalid until the name is filled in.
Exercise
A teammate writes const f = form(model); and then reads the email with f.email.value(). The code does not compile. What is wrong, and what is the correct expression? Also, why would creating form(model) inside a computed() cause problems?
Show answer
f.email is the child field, not its state, so it has no value to call. Call the field first to get its state: f.email().value(). Creating a form inside computed() rebuilds the form each time the computed re-evaluates, which discards the form's interaction state. Create the form once, in a field initializer or constructor, and read it from the computed instead.