Sub topic 2
Start with a signal model
This chapter assumes you know TypeScript interfaces, Angular standalone components, and the basics of signal(), computed(), and effect(). Signal Forms is experimental in Angular 21, so names and details may change in later releases.
Every Signal Form begins with a plain writable signal that holds the form's data. This signal is the model. The form you build later reads from it and writes back to it. This chapter covers how to design that model, how it stays in sync with the form, and which mistakes to avoid.
The model is the source of truth
Reactive Forms keeps its own copy of the data inside FormControl and FormGroup objects. You read or write values through those objects. Signal Forms removes that copy. The data lives in one signal that you own, and the form is a layer of state built on top of it.
import { signal } from '@angular/core';
interface ProfileModel {
name: string;
email: string;
age: number | null;
newsletter: boolean;
}
const profile = signal<ProfileModel>({
name: '',
email: '',
age: null,
newsletter: false,
});Nothing here is specific to forms. profile is a normal signal, so you can read it with profile(), update it with set() or update(), and use it in computed() and effect(). The next chapter passes this signal to form().
The model has three useful properties:
- It is typed. The interface decides which fields exist and what type each one has.
- It is the only copy of the data. Nothing needs to be synchronized by hand.
- It is serializable. You can send
profile()to a server as it is, because it contains plain values and no control objects.
How the model and the form stay in sync
After you create a form from a model, the sync runs both ways.
- When code changes the model, for example with
profile.set(...), the form fields show the new values. - When the user types in a bound input, the form writes the new value into the model signal.
Because the model is an ordinary signal, you can use it directly without going through the form. This example derives a display value from the model:
import { computed, signal } from '@angular/core';
interface NameModel {
first: string;
last: string;
}
const person = signal<NameModel>({ first: 'Ada', last: 'Lovelace' });
const fullName = computed(() => `${person().first} ${person().last}`.trim());
console.log(fullName()); // "Ada Lovelace"
person.update((current) => ({ ...current, first: 'Augusta' }));
console.log(fullName()); // "Augusta Lovelace"Once a form wraps person, typing in a bound first-name input updates fullName in the same way, with no extra wiring.
Design the model for the form
The model's shape becomes the form's shape. Each property becomes a field, and each nested object becomes a group of fields. So design the model first and treat it as the plan for the form.
Use values the inputs can hold
Give every property a defined starting value. Signal Forms builds its field tree from the model's current shape, so a property that is missing or undefined is a poor starting point. Pick a starting value that matches how the input behaves:
| Input kind | Type | Typical starting value | Column |
|---|---|---|---|
| Text, email, password | `string` | `''` | |
| Checkbox | `boolean` | `false` | |
| Number input | `number \ | null` | `null` |
| Select with no choice yet | `string` | `''` | |
| Date as text | `string` | `''` |
A number field is the usual surprise. An empty text box has the value '', which is not a number. Typing the property as number | null lets "nothing entered yet" be a real, honest state.
Nest related fields
Group fields that belong together, such as an address. The form mirrors the nesting.
import { signal } from '@angular/core';
import { form } from '@angular/forms/signals';
interface CheckoutModel {
customer: {
name: string;
email: string;
};
shipping: {
street: string;
city: string;
postalCode: string;
};
giftWrap: boolean;
}
const checkoutModel = signal<CheckoutModel>({
customer: { name: '', email: '' },
shipping: { street: '', city: '', postalCode: '' },
giftWrap: false,
});
const checkoutForm = form(checkoutModel);
// Fields are reached by following the same property path as the model.
console.log(checkoutForm.shipping.city().value()); // ""Each field is reached with the same property path as the model, so checkoutForm.shipping.city matches checkoutModel().shipping.city. Calling the field, as in checkoutForm.shipping.city(), returns its state object. That state exposes value() and the other signals covered later in the tutorial.
Keep form state out of the model
Put only the data you want to save in the model. Validity, touched flags, and error lists belong to the form, not to the model. If you add fields such as isValid or hasBeenEdited to your interface, you duplicate state the form already tracks and create two copies that can disagree.
Reading and writing through the form
You can change data from either side. Both calls below end up in the same place:
import { signal } from '@angular/core';
import { form } from '@angular/forms/signals';
const model = signal({ name: '', newsletter: false });
const f = form(model);
// Write through the model.
model.update((m) => ({ ...m, name: 'Grace' }));
console.log(f.name().value()); // "Grace"
// Write through a field.
f.name().value.set('Grace Hopper');
console.log(model().name); // "Grace Hopper"The field's value is a writable signal. Setting it updates the matching property in the model.
Use whichever side fits the situation. Use a field when you react to a single input. Use the model when you load data from a server or reset the whole form, because one set() replaces everything at once.
Loading and resetting data
Loading saved data is a model write:
import { signal } from '@angular/core';
interface Settings {
displayName: string;
darkMode: boolean;
}
const emptySettings = (): Settings => ({ displayName: '', darkMode: false });
const settings = signal<Settings>(emptySettings());
function loadFromServer(saved: Settings) {
settings.set({ ...saved });
}
function clearValues() {
settings.set(emptySettings());
}The factory function emptySettings() returns a fresh object each time. That matters for the next section.
Edge cases and common errors
- Mutating the model in place. Writing
model().name = 'x'changes the object but does not notify the signal, so nothing updates. Always replace the object withset()orupdate()and a spread copy. - Sharing one default object. If you reuse a single constant as the initial value and as the "reset" value, later edits can alter the constant. Return a new object from a function, as
emptySettings()does. - Changing the shape later. Adding a property to the model after the form exists is a poor way to add a field. Decide on all the properties up front and give each one a starting value.
- Using
undefinedfor "empty". Prefer'',false, ornull. These values keep the property present in the model, which keeps its field in the form. - Wrongly typed numbers. A model typed as
numberwith a starting value of0shows "0" in the input and can look like user input. Usenumber | nullwhen blank is a valid state. - Putting derived data in the model. A total price computed from quantity and unit price should be a
computed()signal, not a model property the user could edit out of step.
Using the model in a component
In a component, make the model a private field and expose the form to the template. Keeping the model private lets the rest of the class read it while the template talks only to the form.
import { Component, computed, signal } from '@angular/core';
import { form } from '@angular/forms/signals';
interface OrderModel {
quantity: number | null;
unitPrice: number;
}
@Component({
selector: 'app-order-summary',
template: `<p>Total: {{ total() }}</p>`,
})
export class OrderSummaryComponent {
private readonly model = signal<OrderModel>({ quantity: null, unitPrice: 12 });
protected readonly orderForm = form(this.model);
protected readonly total = computed(
() => (this.model().quantity ?? 0) * this.model().unitPrice,
);
}The total signal reads from the model and updates whenever a bound quantity input changes. The component does not subscribe to anything.
Summary
- A Signal Form starts from a writable signal that holds all the form data.
- The model's interface decides the field tree. Nested objects become nested groups.
- Changes flow both ways between the model and the bound inputs.
- Give each property a defined starting value, and use
nullfor empty numbers. - Replace the model object instead of mutating it, and keep validity and touched state out of it.
Exercise
A teammate defines const model = signal({ quantity: 0 }); for a number input, and later calls model().quantity = 5; to change the value. Name two problems with this model and how you would fix each.
Show answer
First, mutating model().quantity in place does not notify the signal, so the form and any computed values will not update. Fix it with model.update((m) => ({ ...m, quantity: 5 })), or by calling value.set(5) on the field. Second, a starting value of 0 makes "nothing entered" look like a real answer. Type the property as number | null and start it at null, so a blank input is an honest state.
Exercise
You need a form for a user with a name, an email, and a mailing address with street and city. Write the model interface and the initial signal, then give the path you would use to read the city field after calling form() on a model named userModel into a form named userForm.
Show answer
Interface: interface UserModel { name: string; email: string; address: { street: string; city: string } }. Signal: const userModel = signal<UserModel>({ name: '', email: '', address: { street: '', city: '' } });. After const userForm = form(userModel);, read the city with userForm.address.city().value().