Sub topic 7
Two-Way Binding and the Banana-in-a-Box Syntax
This chapter assumes you already know property binding ([prop]="expr"), event binding ((event)="handler()"), and template reference variables from the earlier chapters. Here you will see how Angular combines those two one-way flows into a single two-way binding, and what the [( )] syntax actually compiles to.
Two flows in opposite directions
Property binding moves data from the component to the template. Event binding moves data from the template back to the component. Two-way binding keeps one value in sync in both directions: when the component field changes, the view updates, and when the user changes the view, the field updates.
You can build this by hand with tools you already know:
import { Component } from '@angular/core';
@Component({
selector: 'app-manual-sync',
template: `
<input #box [value]="name" (input)="name = box.value">
<p>Hello, {{ name }}!</p>
<button (click)="name = 'Grace'">Reset to Grace</button>
`,
})
export class ManualSyncComponent {
name = 'Ada';
}Typing into the box runs the (input) handler, which writes box.value into name. Clicking the button changes name, and the [value] binding pushes the new value into the input. These are two separate bindings that happen to target the same field.
What `[( )]` means
Angular has a shorthand for this pattern. Written [(x)]="field", it is nicknamed "banana in a box": the parentheses are the banana and the square brackets are the box. The compiler rewrites it into two bindings:
| You write | Angular generates |
|---|---|
| `[(x)]="field"` | `[x]="field" (xChange)="field = $event"` |
| `[(ngModel)]="name"` | `[ngModel]="name" (ngModelChange)="name = $event"` |
| `[(size)]="fontSize"` | `[size]="fontSize" (sizeChange)="fontSize = $event"` |
So [(x)] only works when the target has an input named x and an output named xChange. The rule depends on the names alone. Native elements such as <input> have a value property but no valueChange event, so [(value)] on a plain input does not work. You need a directive that supplies the matching pair, and ngModel is that directive for form controls:
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-desugared',
imports: [FormsModule],
template: `
<input [(ngModel)]="name">
<input [ngModel]="name" (ngModelChange)="name = $event">
<p>Both inputs edit: {{ name }}</p>
`,
})
export class DesugaredComponent {
name = 'Ada';
}Both inputs behave the same, because the first one compiles into the second. The long form helps when you need to change the value on its way in, for example (ngModelChange)="name = $event.trim()". The next chapter covers ngModel in detail.
Common errors
- Binding to something that can't be assigned. The generated handler runs
field = $event, so the right-hand side has to be assignable.[(x)]="getName()"or[(x)]="user?.name"fails. Use a plain property, or a writable signal: since Angular 17,[(x)]="mySignal"(written without parentheses) calls.set()for you. - A mismatched event name. If a child component exposes
valueand an output calledchanged,[(value)]will not hear it. The output must be named exactlyvalueChange. - Forgetting
FormsModule. Without it,[(ngModel)]produces "Can't bind to 'ngModel' since it isn't a known property of 'input'". - Using two-way binding everywhere. If data only flows one way, use one-way binding. It makes the data flow easier to trace.
Exercise
A child component declares an input checked and an output checkedChange. Rewrite <app-toggle [(checked)]="isOn" /> without the banana-in-a-box syntax, and explain why [(checked)]="settings.isOn()" would fail.
Show answer
The long form is <app-toggle [checked]="isOn" (checkedChange)="isOn = $event" />. The second version fails because Angular would generate settings.isOn() = $event, and a function call result is not an assignable target. Two-way binding needs a writable property or a writable signal.
Exercise
Why doesn't <input [(value)]="name"> keep name in sync when the user types?
Show answer
The shorthand expands to [value]="name" (valueChange)="name = $event". A native input element never emits a valueChange event, so the event half never runs. Use [(ngModel)] with FormsModule, or bind [value] and (input) by hand.