CPCODELAB
Angular

Two-Way Binding with ngModel

8 min read

Two-Way Data Binding

Two-way binding synchronizes a component property with an input field in both directions: changes in the component update the input, and user input updates the component. Angular uses the [(ngModel)] syntax — nicknamed "banana in a box" because of its [()] shape.

ts
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-name-editor',
  standalone: true,
  imports: [FormsModule],
  template: `
    <label>Name:
      <input [(ngModel)]="name" placeholder="Enter name" />
    </label>
    <p>Hello, {{ name }}!</p>
    <button (click)="clear()">Clear</button>
  `
})
export class NameEditorComponent {
  name = 'Angular';

  clear() {
    this.name = '';
  }
}

You must import FormsModule into the component's (or module's) imports array to use ngModel. Without it, Angular will silently ignore the directive.

Under the hood, [(ngModel)] desugars to [ngModel]="name" (ngModelChange)="name = $event". You can write it this long form when you need to run additional logic on change. Two-way binding is most useful for template-driven forms and simple interactive widgets.

Ready to test yourself?
Practice Angular— quiz & coding exercises