Sub topic 10
Two-way binding with model()
This chapter assumes you know Angular standalone components, property binding [prop] and event binding (event), and how to read a signal by calling it. You should also have seen input() and output() from earlier chapters.
Why model() exists
Some components own a value that both the parent and the child change. A toggle switch, a text field, and a rating widget are common examples. With input() and output() you would declare an input for the value and an output that announces each change, then wire both in the parent.
model() combines the two. It creates a writable signal that works as an input, and it also creates an output named after it with the suffix Change.
Declaring a model
import { Component, model } from '@angular/core';
@Component({
selector: 'app-toggle',
template: `
<button (click)="toggle()">{{ checked() ? 'On' : 'Off' }}</button>
`,
})
export class Toggle {
checked = model(false);
toggle() {
this.checked.update((value) => !value);
}
}The field checked does two jobs:
- The parent can set it as an input named
checked. - Each time the child changes it, Angular emits a
checkedChangeevent to the parent.
You read it by calling it, as with any signal: checked(). Unlike an input(), you can write to it with set() and update(). This is the one kind of input a component may change itself.
Binding it from the parent
The parent uses the "banana-in-a-box" syntax [(prop)]:
<app-toggle [(checked)]="isOn" />This is shorthand for a property binding plus an event binding:
<app-toggle [checked]="isOn" (checkedChange)="isOn = $event" />isOn can be a plain property or a writable signal. With a signal, pass the signal itself, not its value: [(checked)]="isOn", not isOn().
Using the model alone or one-way
A model is still an input. A parent can use one-way binding when it only wants to set the initial value and ignore changes:
<app-toggle [checked]="true" />A parent can also listen without binding the value:
<app-toggle (checkedChange)="log($event)" />Required models
When the parent must supply a value, use model.required():
import { Component, model } from '@angular/core';
@Component({
selector: 'app-rating',
template: `
<button (click)="value.set(value() + 1)">Rate {{ value() }}</button>
`,
})
export class Rating {
value = model.required<number>();
}If the parent omits value, Angular reports a compile-time error. Reading a required model before it is set throws at run time.
Aliases
Like input(), model() accepts an alias. The alias changes both names: with model(false, { alias: 'on' }), the parent writes [(on)] and the output becomes onChange.
Common mistakes
- Writing
[(checked)]="isOn()". The right side of a two-way binding must be assignable, so pass the signal or property, not a call. - Updating the model in the parent's constructor and expecting the child to see it before its first render. Values arrive when the template is bound, not earlier.
- Using
model()for a value that only flows down. A plaininput()communicates intent better and does not add an output.
Exercise
A component declares selected = model(''). Write the parent template binding that keeps a parent property choice in sync with it in both directions, then name the output event Angular creates.
Show answer
Use <app-picker [(selected)]="choice" />. Angular creates an output named selectedChange, and the binding expands to [selected]="choice" (selectedChange)="choice = $event".
Exercise
Why does [(checked)]="isOn()" fail when isOn is a signal?
Show answer
A two-way binding must write back to its target, and isOn() is the result of a read, not something Angular can assign to. Bind isOn itself so Angular can call set() on the signal.