Sub topic 7
Two-Way Binding with model()
This chapter assumes you already know signal inputs from input(), writable signals with set() and update(), and Angular's property and event binding syntax.
A signal input is read-only inside the child. The parent owns the value, and the child can only read it. Some components need the child to change the value too. A toggle, a rating widget, and a custom text field all work this way. model() handles this case. It declares an input that the child can also write, and Angular sends every write back to the parent.
Declaring a model input
model() returns a ModelSignal, which is a writable signal that is also an input. You read it by calling it, and you change it with set() or update().
import { Component, model } from '@angular/core';
@Component({
selector: 'app-toggle',
template: `
<button (click)="toggle()">{{ checked() ? 'On' : 'Off' }}</button>
`,
})
export class ToggleComponent {
checked = model(false);
toggle() {
this.checked.update(v => !v);
}
}Use model.required<boolean>() when the parent must always supply a value. It works like input.required().
Binding from the parent
The parent uses banana-in-a-box syntax, [( )], and passes in a writable signal:
import { Component, signal } from '@angular/core';
import { ToggleComponent } from './toggle.component';
@Component({
selector: 'app-settings',
imports: [ToggleComponent],
template: `
<app-toggle [(checked)]="notifications" />
<p>Notifications: {{ notifications() ? 'on' : 'off' }}</p>
`,
})
export class SettingsComponent {
notifications = signal(false);
}When the child calls checked.update(), the notifications signal in the parent changes too, and both templates update.
The parent can also bind a plain property, such as notifications = false. Angular writes the new value to that property. Binding a signal is the better choice in signal-based code, because other computed() values in the parent can then depend on it.
What the compiler generates
For every model() property, Angular also creates an output named after the property plus Change. For checked, that output is checkedChange. [(checked)]="notifications" is shorthand for one input binding and one event binding:
<app-toggle [checked]="notifications()" (checkedChange)="notifications.set($event)" />You can write this long form when you need to run extra logic on each change. The output fires only when the child writes to the model. It does not fire when the parent pushes a new value in.
input() versus model()
| Feature | `input()` | `model()` |
|---|---|---|
| Child can write | No | Yes, with `set()` and `update()` |
| Generated output | None | `<name>Change` |
| Parent binding | `[name]` | `[name]` or `[(name)]` |
| Typical use | Configuration and display data | Values the user edits in the child |
Common errors
- Calling the signal in the binding.
[(checked)]="notifications()"passes a boolean, not the signal, and the compiler rejects it because there is nothing to write back to. Pass the signal itself:[(checked)]="notifications". - Using model() for one-way data. If the child never writes the value, use
input(). That way the component's API makes clear who owns the state. - Mutating objects in place.
checked().items.push(x)changes the object without callingset(), so noChangeevent fires and signal equality sees no change. Write a new object or array withupdate().
Exercise
A component declares query = model(''). Write the long-form parent binding equivalent to <app-search [(query)]="term" />, where term is a writable signal, and explain when queryChange fires.
Show answer
<app-search [query]="term()" (queryChange)="term.set($event)" />. queryChange fires only when the child writes to query with set() or update(). It does not fire when the parent changes term.
Exercise
Why does [(query)]="term()" fail when term is a signal?
Show answer
term() evaluates to a string, which cannot be assigned to. Two-way binding needs a writable target, either the signal itself or a plain property, so that Angular can write the emitted value back.