0Pricing
Angular Academy · Lesson

Two-Way Binding with ngModel

Sync form inputs with component state.

Two-Way Binding with ngModel is a free Angular Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Two-Way Binding?

Two-way binding keeps a value in sync between the component and the view: updating one updates the other. The syntax is the banana-in-a-box [()].

The Banana in a Box

The combined [(...)] syntax is shorthand for a property binding plus an event binding. The brackets are the box, the parentheses the banana.

<!-- shorthand -->
<input [(ngModel)]="name">

<!-- equivalent to -->
<input [ngModel]="name" (ngModelChange)="name = $event">

Introducing ngModel

ngModel is a directive for two-way binding on form controls like inputs, selects, and textareas.

<input [(ngModel)]="username">
<p>Hello, {{ username }}</p>

Importing FormsModule

To use ngModel you must import FormsModule in your standalone component or NgModule.

import { FormsModule } from "@angular/forms";

@Component({
  selector: "app-demo",
  standalone: true,
  imports: [FormsModule],
  template: "<input [(ngModel)]='name'>"
})
export class DemoComponent {
  name = "";
}

Binding a Text Input

As the user types, the bound property updates immediately, and changing the property in code updates the input.

<input [(ngModel)]="email">
<p>Current: {{ email }}</p>

Binding a Checkbox

ngModel works with checkboxes, binding to a boolean.

<input type="checkbox" [(ngModel)]="agreed">
<p>Agreed: {{ agreed }}</p>

// component
agreed = false;

Binding a Select

With a select element, ngModel binds to the chosen option value.

<select [(ngModel)]="color">
  <option value="red">Red</option>
  <option value="blue">Blue</option>
</select>
<p>Picked: {{ color }}</p>

ngModel Inside Forms

Inside a template-driven form, add a name attribute so Angular registers the control with the form.

<form #f="ngForm">
  <input name="city" [(ngModel)]="city">
</form>

Standalone ngModel

Outside a form, ngModel works on its own as long as FormsModule is imported; no name is required.

<input [(ngModel)]="search">

When to Use ngModel

ngModel shines for simple template-driven forms. For complex validation and dynamic forms, reactive forms are often preferred.

Reacting to Changes

If you only need to react to changes, bind the (ngModelChange) event directly instead of using full two-way binding.

<input [ngModel]="q" (ngModelChange)="onSearch($event)">

Quick Check

Test your understanding of ngModel two-way binding.

Recap

Two-way binding uses the banana-in-a-box [(ngModel)] syntax, which combines property and event binding. Import FormsModule to use it on inputs, checkboxes, and selects.

Frequently asked questions

Is the “Two-Way Binding with ngModel” lesson free?

Yes — the full text of “Two-Way Binding with ngModel” is free to read here on the web, and the Angular Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Angular Academy course, upgrade to CoddyKit PRO.

What will I learn in “Two-Way Binding with ngModel”?

Sync form inputs with component state. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Angular Academy?

No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Two-Way Binding with ngModel” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Angular Academy lesson?

Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Property and Event Binding Recap
  2. Two-Way Binding with ngModel
  3. model() Signal Inputs
  4. Custom Two-Way Bindable Properties
← Back to Angular Academy