Sub topic 12
Building Components That Support Two-Way Binding
This chapter assumes you already know the banana-in-a-box syntax [(x)], @Input/@Output, and the signal-based input(), output() and model() APIs from earlier chapters. Here you will put those pieces together so that your own components accept [(something)] the same way form controls accept [(ngModel)].
The Naming Contract
Angular does not treat two-way binding as a separate feature. The compiler rewrites [(size)]="fontSize" into two one-way bindings:
<app-sizer [size]="fontSize" (sizeChange)="fontSize = $event" />So any component works with [(size)] if it has:
- An input named
size. - An output named
sizeChangethat emits the new value.
The output must be named exactly the input name plus Change. If the name is off by one letter, such as sizeChanged, the template compiler reports that the property or event doesn't exist on the component.
The Decorator Version
import { Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'app-sizer',
template: `
<button (click)="change(-1)">-</button>
<span [style.font-size.px]="size">{{ size }}px</span>
<button (click)="change(1)">+</button>
`,
})
export class SizerComponent {
@Input() size = 16;
@Output() sizeChange = new EventEmitter<number>();
change(delta: number) {
this.size = Math.min(40, Math.max(8, this.size + delta));
this.sizeChange.emit(this.size);
}
}
@Component({
selector: 'app-root',
imports: [SizerComponent],
template: `
<app-sizer [(size)]="fontSize" />
<p>Parent font size: {{ fontSize }}</p>
`,
})
export class AppComponent {
fontSize = 16;
}The child updates its own copy and then emits. If you only update this.size and skip emit, the child shows the new value and the parent keeps the old one. Nothing reports an error when this happens.
The model() Version
model() creates a writable signal that acts as both the input and the Change output. When you call set() or update() on it, Angular emits sizeChange for you.
import { Component, model, signal } from '@angular/core';
@Component({
selector: 'app-sizer',
template: `
<button (click)="change(-1)">-</button>
<span [style.font-size.px]="size()">{{ size() }}px</span>
<button (click)="change(1)">+</button>
`,
})
export class SizerComponent {
size = model(16);
change(delta: number) {
this.size.update(s => Math.min(40, Math.max(8, s + delta)));
}
}
@Component({
selector: 'app-root',
imports: [SizerComponent],
template: `
<app-sizer [(size)]="fontSize" />
<p>Parent font size: {{ fontSize() }}</p>
`,
})
export class AppComponent {
fontSize = signal(16);
}The parent can bind either a plain field or a writable signal. With a signal, Angular writes to it directly. That is why you pass fontSize, not fontSize().
Choosing an Approach
| Concern | `@Input` + `@Output` | `model()` |
|---|---|---|
| Boilerplate | Two members, manual `emit` | One member |
| Forgetting to notify the parent | Easy to do | Not possible: writes emit automatically |
| Reading the value | `this.size` | `this.size()` |
| Works with `computed()`/`effect()` | No | Yes |
In new code, prefer model(). Use the decorator pair when you are working in an existing codebase that doesn't use signals yet.
Common Errors
- Using
input()instead ofmodel(). Signals created byinput()are read-only, so callingseton one is a type error. Useinput()only when data flows one way. - Emitting on every input change. If the child emits from
ngOnChangesor aneffect()that watches its own input, the parent and child can trigger each other in a loop. Emit only in response to user actions. - Requiring a value.
model.required<number>()forces the parent to bind it, which suits components that have no sensible default.
Exercise
A teammate's component has @Input() open = false; and @Output() openChanged = new EventEmitter<boolean>();. The parent writes <app-panel [(open)]="isOpen" /> and gets a compile error. What is wrong, and what is the one-line model() replacement?
Show answer
The output must be named openChange (the input name plus "Change"), not openChanged. Replace both members with open = model(false); and toggle it with this.open.update(v => !v), which emits openChange automatically.
Exercise
The parent declares count = signal(0) and binds <app-counter [(count)]="count()" />. Why does this fail, and how do you fix it?
Show answer
count() returns a number, and you can't assign the emitted value to a number. Angular needs a writable target, so bind the signal itself: [(count)]="count".