Sub topic 7
Two-Way Binding with model()
Signal inputs give a component a read-only view of data its parent owns. Sometimes the child also needs to change that value. Common cases are a toggle switch, a quantity stepper, a date picker, or a tab strip that reports which tab is active. Before signals, you built this by pairing an @Input() named value with an @Output() named valueChange and keeping the two in sync by hand. The model() function does both jobs in one declaration.
This chapter assumes you already know signal(), computed(), input(), and output() from earlier chapters, and that you have used Angular's [(ngModel)] or another banana-in-a-box binding before.
The working model
A model input is a writable signal that is also an input and an output. The parent can pass a value in, as it would for any input. The child can call set() or update() on it, as it would on any writable signal. When the child writes, Angular emits a change event so the parent can update its own state.
| Declaration | Child can read | Child can write | Parent is notified of child writes |
|---|---|---|---|
| `input()` | Yes | No | No |
| `input()` + `output()` | Yes | No (it emits a new value and waits for the parent) | Yes, if the parent listens |
| `model()` | Yes | Yes | Yes, automatically |
model() returns a ModelSignal<T>. That type implements WritableSignal<T> and InputSignal<T>, and it carries an output. Angular names the output by appending Change to the property name. A model called value produces an output called valueChange, and a model called checked produces checkedChange. This naming convention is what makes the [( )] syntax work.
Building a component with model()
Here is a quantity stepper. The value model holds the current count, and a regular min input sets a floor.
import { Component, input, model } from '@angular/core';
@Component({
selector: 'app-stepper',
template: `
<button (click)="step(-1)" [disabled]="value() <= min()">-</button>
<span>{{ value() }}</span>
<button (click)="step(1)">+</button>
`,
})
export class StepperComponent {
value = model(1);
min = input(0);
step(delta: number) {
this.value.update(v => Math.max(this.min(), v + delta));
}
}Inside the class, value behaves like any writable signal. You read it with value(), write it with set() or update(), and use it in computed() or effect(). The default 1 applies only when the parent doesn't bind value.
Binding from the parent
The parent can bind to a model in three ways.
import { Component, computed, signal } from '@angular/core';
import { StepperComponent } from './stepper.component';
@Component({
selector: 'app-order',
imports: [StepperComponent],
template: `
<!-- 1. Two-way binding to a writable signal -->
<app-stepper [(value)]="quantity" />
<p>Total: {{ total() }}</p>
<!-- 2. Two-way binding to a plain property -->
<app-stepper [(value)]="giftWraps" />
<p>Gift wraps: {{ giftWraps }}</p>
<!-- 3. One-way input plus an explicit listener -->
<app-stepper [value]="quantity()" (valueChange)="onChange($event)" />
`,
})
export class OrderComponent {
quantity = signal(2);
total = computed(() => this.quantity() * 9.5);
giftWraps = 0;
onChange(next: number) {
console.log('Stepper changed to', next);
this.quantity.set(next);
}
}Binding to a writable signal
When the target is a WritableSignal, you pass the signal itself, without calling it. Angular reads the signal to feed the input and calls set() on it when valueChange fires. Because quantity is a signal, total recomputes and the template updates. No extra wiring is needed.
Binding to a plain property
When the target is a plain field, [(value)]="giftWraps" expands to [value]="giftWraps" (valueChange)="giftWraps = $event". This form works with existing code that hasn't moved to signals yet.
Splitting the binding
The third form shows what the banana box expands to for a signal: [value]="quantity()" plus a (valueChange) handler. Use it when you need to validate, log, or transform the value before you store it.
Variants and options
Required models
Use model.required<T>() when the child has no sensible default and the parent must supply a value. Angular reports a compile-time error if a parent omits the binding. Reading the model before the first input value arrives, for example in a field initializer or the constructor, throws a runtime error.
import { Component, model } from '@angular/core';
@Component({
selector: 'app-toggle',
template: `
<button role="switch" [attr.aria-checked]="checked()" (click)="checked.set(!checked())">
{{ checked() ? 'On' : 'Off' }}
</button>
`,
})
export class ToggleComponent {
checked = model.required<boolean>();
}The parent writes <app-toggle [(checked)]="notificationsEnabled" />.
Aliases
An alias changes the public name and also the derived output name:
count = model(0, { alias: 'qty' });Parents now bind [(qty)], and the event is qtyChange. Inside the class the property is still count.
No transforms
input() accepts a transform option. model() does not. A transform would break the round trip, because the value the parent sends in would differ from the value the child sends back. If you need coercion, for example accepting '' as true for a boolean attribute, use a plain input() with a transform and a separate output().
How it behaves at the edges
Writes that don't change the value do not emit
Calling set() with a value equal to the current one, compared with Object.is by default, changes nothing and emits nothing. Clicking "-" when the stepper is already at min clamps to the same number, so the parent sees no event. This matches the signal equality rules covered in the chapter on fine-tuning reactivity.
Parent writes do not emit
valueChange fires only when the child writes to the model. When the parent pushes a new value through the binding, the model updates silently. This prevents feedback loops. It also means you can't use (valueChange) to observe every change to the value. If you need that, read the parent's own signal.
One-way binding lets the two sides diverge
With [value]="quantity()" and no listener, the child can still write to its model. Those writes stay local. The parent's quantity is unchanged, so Angular doesn't re-send it, and the child keeps showing its own number. The next time quantity changes, the parent's value overwrites the child's local edit. This is sometimes what you want, for example a field that starts from a suggestion. When it isn't what you want, it causes "my value randomly reset" bugs.
Don't declare a matching output yourself
If you add valueChange = output<number>() next to value = model(), the two names collide. The model already owns that output.
Prefer computed over syncing effects
It is tempting to write an effect() that copies one model into another signal, or that writes back to the model after "fixing" it. That approach causes extra change detection passes and can loop. Derive read-only values with computed(), and do clamping or normalizing inside the method that calls set(), as step() does above.
Subscribing in code
Because a model carries an output, code that holds a component reference can call subscribe() on it, for example ref.instance.value.subscribe(v => ...) when you create the component dynamically or test it. The subscription returns an object with unsubscribe(), and Angular cleans it up when the component is destroyed.
model() and forms
model() does not make a component work with [(ngModel)] or formControlName. Template-driven and reactive forms still talk to custom controls through ControlValueAccessor. Use model() for component-to-component state. Use ControlValueAccessor when the component must plug into the forms API. Angular's newer experimental signal-based forms do use model inputs for custom controls, but the details are still experimental and may change between releases.
Exercises
Exercise
A parent template contains <app-stepper [(value)]="count()" />, where count is a signal(0). What happens, and how do you fix it?
Show answer
Compilation fails because a two-way binding target must be assignable, and count() is a call expression. Bind the signal itself: [(value)]="count". Angular then reads count() for the input and calls count.set($event) when valueChange fires.
Exercise
A child declares selected = model(0, { alias: 'tab' }). What does the parent write to bind it two-way, and what event name can the parent listen to?
Show answer
The parent writes [(tab)]="activeTab". The derived output takes its name from the alias, so the event is (tabChange). Inside the child the property is still selected.
Exercise
A parent binds <app-stepper [value]="start()" /> with no listener, where start is signal(5). The user clicks "+" three times, then the parent calls start.set(5). What does the stepper show, and why?
Show answer
It shows 8. The clicks write to the child's model locally, and start never changes. start.set(5) sets a value equal to the current one, so the signal doesn't notify, the binding isn't re-evaluated, and the child keeps 8. Only a real change, such as start.set(6), would overwrite the child's value.
Exercise
Rewrite the toggle component so the parent must approve every change before it takes effect. Which APIs do you use instead of model()?
Show answer
Use checked = input.required<boolean>() and checkedChange = output<boolean>(). The button calls this.checkedChange.emit(!this.checked()) and does not write any local state. The parent handles (checkedChange) and decides whether to update the value it passes back through [checked]. Because an input is read-only, the child cannot change the value without the parent's approval.