Sub topic 9
Two-Way Binding with model() and [( )]
This chapter assumes you have read the earlier chapters on property binding, event binding, signals in templates, and component inputs and outputs. Two-way binding combines all four, so it helps to have each of them clear first.
What two-way binding actually is
Two-way binding keeps a value in a parent and a value in a child in sync. The parent passes a value down, and when the child changes it, the new value flows back up. Angular has no separate mechanism for this. The "banana in a box" syntax [( )] is shorthand for one property binding plus one event binding:
<app-toggle [(on)]="notifications" />
<!-- is equivalent to -->
<app-toggle [on]="notifications()" (onChange)="notifications.set($event)" />The convention is fixed. For a two-way binding named on, Angular looks for an input called on and an output called onChange. If either name is missing or wrong, the shorthand does not compile.
Declaring a two-way property with model()
The model() function creates a model input. It is a writable signal that is also an input. Angular generates the matching Change output for you.
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);
}Look at three details here:
- Inside the child,
onacts like any writable signal. You read it withon()and change it withset()orupdate(). - Calling
set()orupdate()on a model updates its local value and emitsonChange. You never callemit()yourself. - In the parent,
[(on)]="notifications"passes the signal itself, without parentheses. Angular reads the signal for the downward binding and writes to it when the event fires.
If no parent binds the model, it still works as local state that starts at its default value. That makes model() a good fit for components that can be either controlled or uncontrolled.
model() compared with input() and output()
| Feature | `input()` | `model()` |
|---|---|---|
| Parent can bind with `[x]` | Yes | Yes |
| Child can write the value | No, `InputSignal` has no `set` | Yes, `set()` and `update()` |
| Auto-generated output | None | `xChange` |
| Supports `[(x)]` | No | Yes |
| Supports `transform` option | Yes | No |
Use input() when the parent alone owns the value. Use model() when the child is meant to edit a value the parent also cares about, as form controls, toggles, sliders, and pickers do.
Required models and aliases
Use model.required<T>() when a parent must always supply the value. Leaving out the binding is then a compile-time error.
import { Component, model, signal } from '@angular/core';
@Component({
selector: 'app-stepper',
template: `
<button (click)="value.update(v => v - step)">-</button>
<span>{{ value() }}</span>
<button (click)="value.update(v => v + step)">+</button>
`
})
export class StepperComponent {
value = model.required<number>();
step = 1;
}
@Component({
selector: 'app-cart-line',
imports: [StepperComponent],
template: `
<app-stepper [(value)]="quantity" />
<p>Total: {{ quantity() * 4.5 }}</p>
`
})
export class CartLineComponent {
quantity = signal(1);
}An alias changes the public name, and the output name follows it. model(false, { alias: 'checked' }) exposes [(checked)] and emits checkedChange, while the class still refers to the property by its own name.
The decorator form
Older code and some libraries use decorators. The same naming rule applies: the output must be named after the input with Change appended.
import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'app-legacy-toggle',
template: `<button (click)="flip()">{{ on ? 'On' : 'Off' }}</button>`
})
export class LegacyToggleComponent {
@Input() on = false;
@Output() onChange = new EventEmitter<boolean>();
flip() {
this.on = !this.on;
this.onChange.emit(this.on);
}
}Here you must update the local field and emit the event yourself. A common bug is to emit without updating the local field, or the reverse, so the two sides drift out of sync. model() does both in one call.
Two-way binding on form elements with ngModel
Native elements such as <input> have no valueChange output, so [(value)] will not work on them. For forms, import FormsModule and use the ngModel directive, which follows the same [( )] pattern:
import { Component, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-name-form',
imports: [FormsModule],
template: `
<input [(ngModel)]="name" placeholder="Your name" />
<p>Hello, {{ name() || 'stranger' }}!</p>
`
})
export class NameFormComponent {
name = signal('');
}Common errors and edge cases
Calling the signal in the binding
<!-- Wrong: a call expression cannot be assigned to -->
<app-toggle [(on)]="notifications()" />The target of [( )] must be something Angular can write to: a writable signal or a plain class property. A function call, a literal, a pipe expression, or a read-only computed() signal is not writable. Pass the signal itself: [(on)]="notifications".
Plain properties also work
[(on)]="enabled", where enabled = false is an ordinary field, compiles and works. Angular assigns enabled = $event. Signals are still preferred, because they integrate with OnPush change detection (covered in a later chapter).
The parent changing the value does not emit
The Change output fires only when the child writes to the model. When the parent updates its own signal, the new value flows down through the input, and nothing is emitted back. This is also why two-way binding cannot loop forever. A write that leaves the value unchanged under the signal's equality check produces no emission either.
Mutating objects in place
If a model holds an object or array, value().push(x) changes the data without calling set(). No change event fires, and the parent's signal still points at the same reference, so dependents are not notified. Always write a new value: items.update(list => [...list, x]).
Combining [( )] with a separate (xChange) handler
You can add (onChange)="log($event)" alongside [(on)]. Both handlers run. This is useful for side effects such as saving, but keep the state change in the two-way binding and put only extra work in the separate handler.
Exercises
Exercise
Rewrite <app-stepper [(value)]="quantity" /> as separate property and event bindings, given that quantity is a WritableSignal<number>.
Show answer
<app-stepper [value]="quantity()" (valueChange)="quantity.set($event)" />. The property binding reads the signal, and the event binding writes the emitted number back into it.
Exercise
A teammate declares on = input(false) in a child and writes this.on.set(true). The parent uses [(on)]="flag". What two problems will they hit, and what is the fix?
Show answer
First, InputSignal is read-only, so set does not exist and the code fails to compile. Second, there is no onChange output, so [(on)] cannot compile either. Declare on = model(false) instead. That makes the signal writable and generates onChange.
Exercise
A parent holds tags = signal<string[]>([]) bound with [(tags)] to a child whose model is tags = model<string[]>([]). The child runs this.tags().push('new'). Why does the parent's displayed count not update, and how should the child add the tag?
Show answer
push mutates the existing array without calling set or update. No change is emitted, and no signal notification happens. Use this.tags.update(list => [...list, 'new']), which creates a new array, notifies readers, and emits tagsChange.