Sub topic 2
Start with a signal model
This chapter assumes you know TypeScript, standalone components, and the basics of signal(), computed() and effect(). Signal Forms is experimental in Angular 21, so names may change in later releases.
The model is the source of truth
In Reactive Forms, the form object holds the data. You build a FormGroup and read values from it. In Signal Forms, you start with a plain writable signal that holds the data. The form is derived from that signal later, with form(), which the next chapter covers.
The model is an ordinary object. It has no form-specific types and no controls. Anything that can read or write a signal can use it.
Define the shape
Describe the data with an interface, then create the signal with an initial value for every field.
import { computed, signal } from '@angular/core';
interface Profile {
name: string;
email: string;
age: number;
subscribed: boolean;
}
const profile = signal<Profile>({
name: '',
email: '',
age: 18,
subscribed: false,
});
const initials = computed(() =>
profile()
.name.split(' ')
.filter(Boolean)
.map((part) => part[0].toUpperCase())
.join('')
);
console.log(initials()); // ''
profile.update((p) => ({ ...p, name: 'Ada Lovelace' }));
console.log(initials()); // 'AL'The computed() signal recalculates when the model changes. It works with no form involved. Later, when the user types into a bound input, the model changes in the same way and initials updates too.
Rules for a good model
The form is built from the model's shape, so the initial value matters.
- Give every field an initial value. A field missing from the object has nothing to bind to.
- Use empty values for blank inputs:
''for text,falsefor checkboxes, and a sensible default for numbers. - Avoid
undefinedfor fields that users fill in. Pick a value that matches the field's type. - Keep the model to data you would save or send. Put view-only state, such as "is the panel open", in separate signals.
- Nest objects to group related fields, such as
address: { street: '', city: '' }.
interface Order {
customer: string;
address: { street: string; city: string };
}
const order = signal<Order>({
customer: '',
address: { street: '', city: '' },
});Common errors
Updating the model by mutation is the most common one. Writing profile().name = 'Ada' changes the object but does not notify the signal, so nothing reacts. Always call set() or update() with a new object, as shown above.
A second error is a model that does not match the form you want. If the template needs a phone field and the model has no phone key, add it to the interface and the initial value first.
A third is loading data asynchronously and setting the model to a partial object. Set the whole model with profile.set(loadedProfile), with every field present.
Exercise
A teammate writes profile().email = '[email protected]' and the computed() signals that depend on profile do not update. What is wrong, and what is the fix?
Show answer
The code mutates the object inside the signal, so the signal never learns that its value changed. Replace the object instead: profile.update((p) => ({ ...p, email: '[email protected]' })), or call profile.set() with a new object.