Data Binding In Angular 2

Introduction

Data binding is one of the most powerful features of software development technologies. Data binding is the connection bridge between View and the business logic (View Model) of the application. Data binding in Angular is the automatic synchronization between Model and the View. When the Model changes, the Views are automatically updated and vice-versa.

There are many ways to bind the data in Angular. Following are the types of data binding in Angular 2.

  • Interpolation
  • One-way binding (unidirectional)
  • Two-way binding
  • Event binding

Interpolation

This is the easiest way of data binding in AngularJS. This is same as Expressions  in Angular 1.x. In interpolation, we need to supply property name in the View template, enclosed in double curly braces, e.g. {{name}}. It is used for one-way binding (Component class to View only).

In the following example, I have defined one property called "name" in Component Class and it is displayed on View template by using interpolation.

app.component.ts

  1. import { Component } from '@angular/core';  
  2. @Component({  
  3.   selector: 'test-app',  
  4.   templateUrl: './app/databinding.html'  
  5. })  
  6. export class AppComponent {   
  7.     name = 'C# Corner';  
  8. }  
databinding.html
  1. <h4>Data binding in Angular 2 Application</h4>  
  2. <div>  
  3.     <h5>Interpolation Example</h5>  
  4.     Hello {{name}} !  
  5. </div>  
Output

Output

One-way binding

In Angular 2, one-way data binding directive is replaced with [property]. The ng-bind directive is used for one-way binding in Angular 1.x. Angular 2.0 uses HTML DOM element property for one-way binding. The square brackets are used with property name for one-way data binding in Angular 2. For example, if we want one-way binding between Model property and template View for textbox, we need to use [value].

app.component.ts
  1. import { Component } from '@angular/core';  
  2. @Component({  
  3.   selector: 'test-app',  
  4.   templateUrl: './app/databinding.html'  
  5. })  
  6. export class AppComponent {   
  7.     name = 'C# Corner';  
  8.     welcomeText = 'Welcome Jignesh!'  
  9. }  
databinding.html
  1. <div>  
  2.     <h5>One way binding Example</h5>  
  3.     Hello <span [innerText] = "name" ></span>!  
  4.     <br/><br/>  
  5.     <input type = 'text'  [value]="welcomeText" />  
  6. </div>  
Output

Output

Two-way binding

The ng-model directive is used for two-way data binding in Angular 1.x, but it is replaced with [(ngModel)] in Angular 2.0. The ngModel directive is part of a built-in Angular module called "FormsModule". So, we must import this module in to the template module before using the ngModel directive.

app.module.ts
  1. import { NgModule }      from '@angular/core';  
  2. import { BrowserModule } from '@angular/platform-browser';  
  3. import { FormsModule } from '@angular/forms';  
  4.   
  5. import { AppComponent }  from './app.component';  
  6.   
  7. @NgModule({  
  8.   imports:      [ BrowserModule, FormsModule],  
  9.   declarations: [ AppComponent],  
  10.   bootstrap:    [ AppComponent ]  
  11. })  
  12. export class AppModule {   
  13. }  
databinding.html
  1. <div>  
  2.     <h5>Two way binding Example</h5>  
  3.     Enter you Name:  <input [(ngModel)]="enterName"  />  
  4.     <br/><br/>  
  5.     <span> WelCome {{enterName}} ! </span>  
  6. </div>  
Output

Output

Two-way binding without ngModel directive

Angular 2 has a feature called "template reference variables". With this feature, we are able to have direct access to an element. The template reference variable is declared by preceding an identifier with a hash/pound character (#).

In the following example, we have declared a template reference variable named "txtName" on the input element. This variable is in reference to the input element. So, we can get the value property of the input element and display it with interpolation.

Here, template is completely self-contained and it does not bind to the component. This solution will not work unless we bind to the element’s event. Angular only updates the bindings if something is done in response to asynchronous events, like keystrokes. This is the reason we bind keyup event to input element and we do nothing with this event. So, we are binding the number 0 with the event.

databinding.html
  1. <div>  
  2.     <h5>Two way binding (without ngModel directive) Example</h5>  
  3.     Enter you Name: <input  #txtName  type = "text"  (keyup)="0" />  
  4.     <br/><br/>  
  5.     <span> WelCome {{txtName.value}} ! </span>  
  6. </div>  
Output

Output

Event Binding

Angular 2.0 directly uses the valid HTML DOM element events. For example, ng-click is now replaced with (click). The round brackets (parentheses) are used with DOM event name for event binding in Angular 2.

In the following example, I have defined button click event and on button click, I have written the message on screen.

app.component.ts
  1. import { Component } from '@angular/core';  
  2. @Component({  
  3.   selector: 'test-app',  
  4.   templateUrl: './app/databinding.html'  
  5. })  
  6. export class AppComponent {   
  7.     messageText = '';  
  8.     onClickMe() {  
  9.         this.messageText = "Hi Reader!";  
  10.     }  
  11. }  
databinding.html
  1. <div>  
  2.     <h5>Event binding Example</h5>  
  3.      <button (click)="onClickMe()">Click me!</button>  
  4.     <br/><br/>  
  5.     <span> {{messageText}} </span>  
  6. </div>  
Output

Conclusion - This article helps us understand the Data Binding approaches in Angular 2.

Up Next
    Ebook Download
    View all
    Learn
    View all