TExcelTAD
Home › Angular › Two-way Binding

Two-way Binding

Property binding sends data down to the view; event binding sends events up to the component. Two-way binding combines both in one piece of syntax, [(target)]="value", so a form field and a component value stay in sync in both directions.

The brackets-inside-parentheses shape is often called "banana in a box": [( )].

Two-way binding with ngModel

For form elements, the ngModel directive from FormsModule provides two-way binding. You can bind it directly to a writable signal:

angular · try it yourself
import { Component, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-name-editor',
  imports: [FormsModule],
  template: `
    <label>Name: <input [(ngModel)]="name" /></label>
    <label><input type="checkbox" [(ngModel)]="subscribed" /> Subscribe</label>
    <p>Hello {{ name() }}! Newsletter: {{ subscribed() ? 'yes' : 'no' }}</p>
    <button (click)="name.set('Guest')">Reset name</button>
  `,
})
export class NameEditor {
  name = signal('Thu');
  subscribed = signal(true);
}

Typing in the input updates name; clicking Reset name updates the input. Note that the binding uses name (the signal itself), not name() — Angular needs the signal so it can write to it.

What the banana in a box expands to

[(x)]="value" is shorthand. Angular rewrites it into a property binding plus an event binding named xChange:

html
<!-- these two lines are equivalent -->
<input [(ngModel)]="title" />
<input [ngModel]="title()" (ngModelChange)="title.set($event)" />

The long form is useful when you need to transform the value on the way in, for example to trim or uppercase it:

html
<input [ngModel]="code()" (ngModelChange)="code.set($event.toUpperCase())" />

Two-way binding on your own components: model()

For your own components, declare a model input with model(). It behaves like a writable signal inside the child, and when the child sets it, the parent's bound signal is updated too:

angular · try it yourself
import { Component, model, signal } from '@angular/core';

@Component({
  selector: 'app-rating',
  template: `
    @for (star of [1, 2, 3, 4, 5]; track star) {
      <span (click)="value.set(star)" [class.on]="star <= value()">★</span>
    }
  `,
  styles: `span { font-size: 26px; color: #ccc; cursor: pointer; } .on { color: #f5a623; }`,
})
export class Rating {
  value = model(0);
}

@Component({
  selector: 'app-root',
  imports: [Rating],
  template: `
    <app-rating [(value)]="score" />
    <p>Your rating: {{ score() }} / 5</p>
  `,
})
export class App {
  score = signal(3);
}

The preview shows the result after clicking the fourth star: the child wrote 4 into its model, and the parent's score signal followed.

A two-way link in plain TypeScript

The contract is simple: the parent passes a value down and listens for change events coming back up. Here is that handshake with plain classes:

ts · try it yourself
class ChildRating {
  value = 0;
  onValueChange: (v: number) => void = () => {};

  click(star: number): void {
    this.value = star;
    this.onValueChange(star); // like emitting "valueChange"
  }
}

class ParentPage {
  score = 3;
  child = new ChildRating();

  constructor() {
    this.child.value = this.score; // [value]="score"
    this.child.onValueChange = (v) => (this.score = v); // (valueChange)="score = $event"
  }
}

const page = new ParentPage();
console.log('start:', page.score, page.child.value);
page.child.click(5);
console.log('after click:', page.score, page.child.value);
start: 3 3
after click: 5 5

When to use which

Situation Use
Simple form field bound to component state [(ngModel)] (template-driven forms)
Large forms with validation and dynamic fields Reactive forms (later lesson)
Your own component that edits a parent value model() + [(name)]
One-way display plus a custom event input() + output()

Note: You must add FormsModule to the component's imports to use ngModel. Forgetting it gives the error "Can't bind to 'ngModel' since it isn't a known property of 'input'".

Test yourself

What is [(size)]="fontSize" shorthand for?

  1. [size]="fontSize()" (sizeChange)="fontSize.set($event)"
  2. (size)="fontSize"
  3. [size]="fontSize" [sizeChange]="fontSize"
  4. {{ size = fontSize }}

Which import is required for [(ngModel)]?

  1. HttpClientModule
  2. RouterModule
  3. FormsModule
  4. CommonModule

Which function declares a two-way bindable input on your own component?

  1. input()
  2. output()
  3. model()
  4. signal()