How to Use Data Binding In Angular

Introduction 

 
Data binding means to transfer (pass) information between two components. Component.ts files or component.html file data binding means to display data on the server using only a few properties of the data. In other words, we can say that data binding displays large data using small parts of the data. That means that data binding helps save the developer time.
 

Types of data binding

  1. String interpolation data binding
  2. Property binding  
  3. Event binding 
  4. Two-way data binding  

String interpolation

 
Interpolation is used to display component data in the view template with double curly braces. We can display all kinds of data in the view string, number, array, list, etc. Interpolation is used for one-way data binding. Interpolation moves data in one direction from our component to HTML elements. Write a variable name in the TS file, put some value on the variable, and then copy the variable name and paste it to the double curly braces like this:
 
<h2> {{variable name}}</h2>
 
Now, I take a variable and put the value through one-way data binding. Then, I copy this variable and go to the HTML component.
 
component.ts  
  1. import { Component } from '@angular/core';  
  2. @Component({  
  3.     selector:'databinding',  
  4.     templateUrl:'./databinding.html'  
  5.   
  6. })  
  7. export class databindingcomponent{  
  8.     oneway:string="one way databinding";  
  9.       
  10. }  
Then put this variable name in to the <h2></h2> tag under the double curly braces
 
component.html
  1. <h2>{{oneway}}</h2>  
 Now save this file and run the project. The output will be shown like this.
 
dd

Property binding

 
Property binding is also known as one-way data binding. In property binding, we define a Variable property to a field (<input tag>, or image URL(img src=” img url”> . Property binding transfers the data component to the template, which is the variable we take in the component.ts file. 
 
Now go to the component.ts file and write some code:
 
component.ts  
  1. import { Component } from '@angular/core';  
  2. import { usermodel } from '../model/usermodel';  
  3. @Component({  
  4.     selector:'databinding',  
  5.     templateUrl:'./databinding.html'  
  6.   
  7. })  
  8. export class databindingcomponent{  
  9.       
  10.     application:String=" property binding";  
  11. }  
After this go to the HTML component and write the code:
  1. <input type="text" [ngModel]="application">  
After that, run the project. The project will show the following output: 
 
 

Event binding

 
Event binding is the other type of data binding. Angular provides event Data binding to perform an event from a keyboard event and a mouse button event.
 
Angular provides us with other types of binding, i.e., event binding, which is used to handle the events raised from the DOM like button click, mouse movement etc. Let’s understand this with the help of an example. 
 
An event performed by the keyboard now goes to the component.ts page. Write the following code and save:
 
component.ts
  1. import { Component } from '@angular/core';  
  2. import { usermodel } from '../model/usermodel';  
  3. @Component({  
  4.     selector:'databinding',  
  5.     templateUrl:'./databinding.html'  
  6.   
  7. })  
  8. export class databindingcomponent{  
  9.      
  10.     event:string="chaman gautam";  
  11. three="Event binding";  
  12. }  
Now go to the HTML page and write the code:
 
component.html
  1. <h2 class ="spen" style="color: salmon">{{three}}</h2>  
  2. <input type="text" [(ngModel)]="three" value="save">  
Now save this code and run your project. After running the project, it will show output like this:
 
Without performing the event picture, it shows like this:
 
After performing the event picture, it shows like this:
 
 
We saw that when we change the textfield, our value will be changed on the display. This event shows a keyboard event. 
 
mouse event 
 
Go to the component.ts page and write the code:
  1. import { Component } from '@angular/core';  
  2. import { usermodel } from '../model/usermodel';  
  3. @Component({  
  4.     selector:'databinding',  
  5.     templateUrl:'./databinding.html'  
  6.   
  7.   
  8. })  
  9. export class databindingcomponent{  
  10.     objuser:usermodel=new usermodel();  
  11.     oneway:string="one way databinding";  
  12.     application:String="property binding"  
  13.     event:string="chaman gautam";  
  14. three="Event binding";  
  15. savedata(){  
  16.     alert();  
  17.     console.log("application")  
  18. }  
  19. }  
Now go to the component.html page and write the following code:
  1. <h2 class ="spen" style="color: royalblue;">Event binding with mouse click</h2>  
  2.   
  3. <input type="button" (click)="savedata()" name="value" value="save">  
Now save and run project.
 
Without performing a mouse event, the image shows this:
 
 
 
 
 
After pressing the button, the event shows like this:
 
 
 
 
 
Now after pressing the OK button, the image shows like the following:
 
 
 
 
You can see with these events that our variable value will be displayed on the console. This event shows the mouse event.
 

Two-way data binding

 
Two-way property binding is another good way to bind data in Angular. Angular provides for two-way data binding. In two-way data, binding data is transferred on both sides, component side to the template side and template side to the component side. Any changes are shown on the template.
 
Two-way data binding is the transfer of data between components. It's shown like this: