Sub topic 9
Two-Way Binding with model() and [( )]
This chapter assumes you already know property binding, event binding, signals, and component inputs and outputs from earlier chapters. Two-way binding combines those pieces. The parent passes a value down, and the child can send a new value back up through the same binding.
How two-way binding works
The [( )] syntax, often called "banana in a box," is shorthand for a property binding plus an event binding. When you write [(on)]="notifications", Angular reads it as two bindings:
[on]sends the parent's value into the child.(onChange)listens for the child's new value and writes it back to the parent.
The event name is always the property name followed by Change. The model() function creates both halves for you. It returns a writable signal that works as an input, and it adds a matching output named <name>Change. When the child calls set() or update() on the model, Angular emits that output with the new value.
A working example
import { Component, model, signal } from '@angular/core';
@Component({
selector: 'app-toggle',
template: `<button (click)="on.set(!on())">{{ on() ? 'On' : 'Off' }}</button>`
})
export class ToggleComponent {
on = model(false);
}
@Component({
selector: 'app-settings',
imports: [ToggleComponent],
template: `
<app-toggle [(on)]="notifications" />
<p>Notifications are {{ notifications() ? 'enabled' : 'disabled' }}.</p>
`
})
export class SettingsComponent {
notifications = signal(false);
}When the button is clicked, the child sets on. Angular emits onChange, and the parent's notifications signal receives the new value, so the paragraph updates. The default value false applies only when the parent binds nothing. Use model.required<boolean>() when the parent must always provide a value.
The longhand form
You can write out both halves yourself. This is useful when you want to run extra logic on each change:
import { Component, signal } from '@angular/core';
import { ToggleComponent } from './toggle.component';
@Component({
selector: 'app-settings-longhand',
imports: [ToggleComponent],
template: `
<app-toggle [on]="notifications()" (onChange)="save($event)" />
`
})
export class SettingsLonghandComponent {
notifications = signal(false);
save(value: boolean) {
this.notifications.set(value);
console.log('Saved:', value);
}
}Common errors
| Mistake | Why it fails | Fix |
|---|---|---|
| `[(on)]="notifications()"` | A function call result cannot be assigned to | Pass the signal itself: `[(on)]="notifications"` |
| Using `input()` instead of `model()` | `input()` signals are read-only in the child | Declare the property with `model()` |
| Naming the output `onChanged` by hand | Angular looks only for `onChange` | Use `model()`, or follow the `xChange` naming rule |
Exercise
A child declares volume = model(50). Write the parent template binding that keeps a parent signal level in sync, then write the equivalent longhand form.
Show answer
Shorthand: <app-slider [(volume)]="level" />. Longhand: <app-slider [volume]="level()" (volumeChange)="level.set($event)" />.
Exercise
A teammate writes <app-toggle [(on)]="isOn()" /> and gets a compile error. What is wrong?
Show answer
[( )] must write a value back, and the result of the call isOn() cannot be assigned to. Bind the signal itself with [(on)]="isOn".