Author avatar

Gaurav Singhal

Keeping Data in Sync with Two-way Binding Using Ngmodel

Gaurav Singhal

  • Nov 12, 2019
  • 4 Min read
  • Nov 12, 2019
  • 4 Min read


Keeping the data in sync between the UI and control is very important in a form. It’s crucial to change the value of the control dynamically and display it on the screen at the same time. In this guide, we’ll look at an example that will explain how to do this in Angular.

How to Sync Data

Syncing data between the control and the UI is essential to create an excellent user experience and easily handle form data. We can do this using NgModel, one of the core features of Angular.


NgModel is a directive that creates the FormControl instance from a domain model and binds it to a form control element.

It uses two kinds of binding:

  1. Property binding
  2. Event binding

Property Binding

Property binding is used to pass the data from the component class (component.ts) and set the value of the element in the front end.

To represent property binding, we use a pair of square brackets [ ] with the field name.

Example : <a [data]="" />

Event Binding

Event binding is used to change the data in the component class after a particular action. To represent event binding, we use a pair of round brackets ( ) with the field name.

Example: <a (eventName)="" />

NgModel works using these two bindings together. First, it passes the data from the component class and set data in FormControl. Second, it passes the data from UI after a specific action is triggered and then changes the value of the control.

Syntax of NgModel

In this section, we’ll use ngModel with HTML tags in Angular applications.

<tag (ngModel)="formControlName">

The tag could be any HTML5 tag, such as <input /> or <textarea>. The NgModel doesn't support tags like <span>, <label>.

Let's get our hands dirty and dive into the code.


2  Username:<input
3    type="text"
4    placeholder="Enter Username"
5    id="username"
6    name="username"
7    [(NgModel)]="username"
8  />
9  <label for="username">{{username}}</label>

Here I've used an input tag, but you can choose any tag of your choice. You must have the name attribute in the tag and the crucial NgModel directive. This helps you to sync the data with the component class file. I'm displaying the username in the next line of the input tag, as reflected below.


1import { Component, DoCheck, OnInit } from "@angular/core";
4  selector: "app-root",
5  templateUrl: "./app.component.html",
6  styleUrls: ["./app.component.scss"]
8export class AppComponent implements OnInit {
9  username: string;
11  ngOnInit() {
12    this.username = "John";
13  }

In the component class file, I've just set the value of the variable username, which is the same as the formControlName. It must be the same; otherwise, it's not going to work.


1import { BrowserModule } from "@angular/platform-browser";
2import { NgModule } from "@angular/core";
3import { FormsModule } from "@angular/forms";
4import { AppComponent } from "./app.component";
7  declarations: [AppComponent],
8  imports: [BrowserModule, FormsModule],
9  providers: [],
10  bootstrap: [AppComponent]
12export class AppModule {}

To work with the ngModule you need to import the FormModule. If you're working with a template-driven form, you need to import FormModule in the app.module.ts.

Now we're going to have a look at the output.

As we set the value of the variable username in the app.component.ts, our output should look like this:

Hello John

Depending on the value of the text box, the label below would change to match it.


In this guide, we've learned about an essential topic of template-driven forms. This will help you with a variety of different projects. You can read more here.

If you want to create a dynamic form, take a look at reactive forms.