Data Binding In Angular 8

Introduction

 
In this article, we will learn about Data Binding in Angular. As we know Data Binding is used for binding the data from View to Component or Component to View. In Angular, we have two different types of Data Binding for binding the data from Component.ts class file to Component.html or Component.html to Component.ts or Vice versa.
 

One way Data Binding

 
In one-way data binding, we have two different types of data binding that are binding of data from Component.html or View to Component.ts class file or Component.ts class file to Component.html.
 
We have different ways of binding the data from Component.ts class file to Component.html as shown below.
  • String Interpolation
  • Property Binding
  • Style Binding
  • Class Binding
  • Attribute Binding etc.
In order to pass the data from Component.html (View) to Component.ts or One-way data binding, we use Event Binding.
 
Another way to bind the data from Component.html or View to Component.ts or Component.ts to Component.html we use
 

Two-way Data Binding.

 

String Interpolation

 
In String Interpolation we bind the data from Component.ts class file to Component.html by using the expression in double curly braces. If we don't pass the data or the component's field in double curly braces then angular treat this as a string value and display the string value to the end-user.
 
For example,
 
Open the Component.ts file and place the below code.
  1. export class Test3Component implements OnInit {    
  2.    firstName: string = 'Khaja';    
  3.    lastName: string = 'Moizuddin';    
  4.    number1: number = 100;    
  5.    number2: number = 200;    
  6.    totalAmount: number = this.number1 + this.number2;    
  7.    fullName: string = this.firstName + this.lastName;    
  8.    fullNameVal:string;    
  9.    totalVal:number;    
  10.    csharpCornerURL: string = 'https://www.c-sharpcorner.com/members/khaja-moiz';    
  11.    myImagePath: string = 'assets/Images/mvp1.jpg';    
  12.    heightImg: number = 50;    
  13.    widthImg:number = 50;    
  14.    onButtonClick: string;    
  15.    onDoubleClick: string;    
  16.     
  17.   getTotalAmount1() {    
  18.     return this.number1 + this.number2;    
  19.  }    
  20.     
  21.   getTotalAmount2() {    
  22. return this.totalVal = this.number1 + this.number2;    
  23.     
  24.   }    
  25.   
  26.   getTotalAmount3() {    
  27.    return this.totalVal = this.totalAmount;    
  28.   }    
  29.   getFullName1() {    
  30.     return this.firstName + this.lastName;    
  31.   }    
  32.   getFullName2() {    
  33.  return this.fullNameVal = this.firstName + this.lastName;    
  34.   }    
  35.   getFullName3() {    
  36.     return this.fullNameVal = this.fullName;    
  37.   }    
  38.   onBtnClick() {    
  39. return this.onButtonClick = 'Welcome ' + this.firstName;    
  40.   }    
  41.   onDblClick(){    
  42.     return this.onDoubleClick = 'Welcome ' + this.lastName;    
  43.   }    
  44. constructor() { }    
  45.   ngOnInit() {    
  46.   }    
  47. }    
Now open the Component.html file and place the below code.
  1. <h2>STRING INTERPOLATION</h2>    
  2. <hr>    
  3. <h3> firstName is:{{this.firstName}}  </h3>    
  4. <h3> lastName is: {{this.lastName}} </h3>    
  5. <h3> fullName is:{{this.fullName}}  </h3>    
  6. <h3> fullName1 is:{{this.getFullName1()}}  </h3>    
  7. <h3> fullName2 is:{{this.getFullName2()}} </h3>    
  8. <h3> fullName3 is:{{this.getFullName3()}}  </h3>    
  9. <h3> Number1 is : {{this.number1}} </h3>    
  10. <h3> Number2 is:{{this.number2}}  </h3>    
  11. <h3> TotalAmount is:{{this.totalAmount}}  </h3>    
  12. <h3> TotalAmount1 is:{{this.getTotalAmount1()}}  </h3>    
  13. <h3> TotalAmount2 is:{{this.getTotalAmount2()}}  </h3>    
  14. <h3>TotalAmount3 is: {{this.getTotalAmount3()}}  </h3>    
Here in the above code we used different ways of binding the data in String Interpolation, we used double curly braces for binding the data from component.ts to component.html with this it converts the field values as expression.
 
In the above we used fullName: string = this.firstName + this.lastName; or totalAmount: number = this.number1 + this.number2; which converts the values as an expressions and returns the result to the end user.
 
The output for String Interpolation is,
 
Data Binding In Angular 8

Property Binding 

 
In Property binding, we bind the data from Component.ts to Component.html. The HTML elements property is bound with the values from Component.ts file using square brackets [].
 
For Example,
 
Now open the Component.html file and place the below code.
  1. <span [innerText]='this.firstName'></span><br>    
  2. <span [innerHTML] = 'this.lastName'></span><br>    
  3. <span innerText = '{{this.firstName}}'></span><br>    
  4. <span innerHTML = '{{this.lastName}}'></span>    
In the above code we used [innerHTML] and [innerText] which is nothing but property binding, here the values this.firstName, this.lastName is binded from the Component.ts file, the Output will be visible to the end user which will be placed in between the span tags.
  1. <!--BINDING PROPERTY TO DOM ELEMENT-->    
  2. <span [attr.innerText]='this.firstName'></span><br>    
  3. <span [attr.innerHTML] = 'this.lastName'></span><br>    
  4. <span attr.innerText = '{{this.firstName}}'></span><br>    
  5. <span attr.innerHTML = '{{this.lastName}}'></span>    
With the above code, we used attr attribute which is nothing but attribute binding with property name which binds the Component's value to the DOM element. The output will not be visible to the end-user but the innerText and innerHTML property will be added to the span tags which can be shown below.
 
Data Binding In Angular 8
  1. <a href="{{this.csharpCornerURL}}" target="_blank">CsharpCorner</a><br>    
  2. <a [href]='this.csharpCornerURL' target="_blank">CsharpCorner</a><br>    
  3. <a attr.href = '{{this.csharpCornerURL}}' target="_blank">CsharpCorner</a><br>    
  4. <a [attr.href]='this.csharpCornerURL' target="_blank">CsharpCorner</a><br>    
  5.     
  6. <img src='{{this.myImagePath}}' height='{{this.heightImg}}' width="{{this.widthImg}}" /><br>    
  7. <img [src]='this.myImagePath' [height]='this.heightImg' [width]="this.widthImg"><br>    
  8. <img attr.src = '{{this.myImagePath}}' attr.height='{{this.heightImg}}' attr.width="{{this.widthImg}}"><br>    
  9. <img [attr.src]='this.myImagePath' [attr.height]='this.heightImg' [attr.width]="this.widthImg">    
Here i have used different ways of binding the Component's value to the property href and src as shown in the above code.
 
The output for the above code is shown below.
 
Data Binding In Angular 8

Style Binding

 
Using Style binding we can set the style using ngStyle attribute by passing the value of the component.ts file. Here I am going to set ngStyle attribute with background color with two conditions that are if the condition is successful then I am going to display the background color with green and if the condition fails then I am going to display the background color with red.
 
Open Component.html file and place the below code.
  1. <h2>NG STYLE WITH TRUE CONDITION</h2>    
  2. <h3 [ngStyle]="{'background-color': this.fullName == this.getFullName1()?'green':'red'}">NG STYLE WITH TRUE CONDITION</h3>    
  3. <h3 [ngStyle] = "{'background-color': this.getFullName1() == this.getFullName2()?'green':'red'}">NG STYLE WITH TRUE CONDITION</h3>    
  4. <h3 [ngStyle] = "{'background-color': this.getFullName3() == this.firstName + this.lastName ?'green':'red'}">NG STYLE WITH TRUE CONDITION</h3>    
  5. <h3 [ngStyle] = "{'background-color': this.totalAmount == this.getTotalAmount1() ?'green':'red'}">NG STYLE WITH TRUE CONDITION</h3>    
  6. <h3 [ngStyle] = "{'background-color': this.getTotalAmount2() == this.getTotalAmount3()?'green':'red'}">NG STYLE WITH TRUE CONDITION</h3>    
  7. <h3 [ngStyle] = "{'background-color': this.getTotalAmount3() == this.number1 + this.number2?'green':'red'}">NG STYLE WITH TRUE CONDITION</h3>    
  8.     
  9. <h2>NG STYLE WITH FALSE CONDITION</h2>    
  10. <h3 [ngStyle]="{'background-color': this.fullName != this.getFullName1()?'green':'red'}">NG STYLE WITH FALSE CONDITION</h3>    
  11. <h3 [ngStyle] = "{'background-color': this.getFullName1() != this.getFullName2()?'green':'red'}">NG STYLE WITH FALSE CONDITION</h3>    
  12. <h3 [ngStyle] = "{'background-color': this.getFullName3() != this.firstName + this.lastName ?'green':'red'}">NG STYLE WITH FALSE CONDITION</h3>    
  13. <h3 [ngStyle] = "{'background-color': this.totalAmount != this.getTotalAmount1() ?'green':'red'}">NG STYLE WITH FALSE CONDITION</h3>    
  14. <h3 [ngStyle] = "{'background-color': this.getTotalAmount2() != this.getTotalAmount3()?'green':'red'}">NG STYLE WITH FALSE CONDITION</h3>    
  15. <h3 [ngStyle] = "{'background-color': this.getTotalAmount3() != this.number1 + this.number2?'green':'red'}">NG STYLE WITH FALSE CONDITION</h3>    
The output for the above code is shown below.
Data Binding In Angular 8

Class Binding

 
Using Class Binding we can set the ngClass attribute by passing the value of the Component.ts file. With ngClass attribute, we can set the class property of the HTML element and can set the CSS for the applied value for the ngClass attribute.
 
Open the Component.html file and place the below code.
  1. NG CLASS EXAMPLE1: <input type="text" [ngClass]= 'this.fullName' [id]='this.firstName' [name]='this.lastName'/><br>    
  2. NG CLASS EXAMPLE1: <input type="text" ngClass= '{{this.fullName}}' id='{{this.firstName}}' name='{{this.lastName}}'/><br><br>    
  3.     
  4. NG CLASS EXAMPLE2: <input type="text" [attr.ngClass]= 'this.fullName' [attr.id]='this.firstName' [attr.name]='this.lastName'/><br>    
  5. NG CLASS EXAMPLE2: <input type="text" attr.ngClass= '{{this.fullName}}' attr.id='{{this.firstName}}' attr.name='{{this.lastName}}'/><br><br>    
  6.     
  7. CLASS EXAMPLE3: <input type="text" [class]= 'this.fullName' [id]='this.firstName' [name]='this.lastName'/><br>    
  8. CLASS EXAMPLE3: <input type="text" class'{{this.fullName}}' id='{{this.firstName}}' name='{{this.lastName}}'/><br><br>    
The output for the above code is shown below.
 
Data Binding In Angular 8
 
The examples explained above are one-way data-binding from Component.ts class file to Component.html or View.
 

Event Binding 

 
It is one-way data binding which is binding the value from Component.html to Component.ts class file or View to Component.
 
Open the Component.html and place the below code.
  1. <h2>EVENT BINDING</h2>    
  2. <hr>    
  3.     
  4. <input type="button" (click)=onBtnClick() value="CLICK" height="20" width="20">    
  5. <h3>{{this.onButtonClick}}</h3>    
  6.     
  7. <input type="button" (dblclick)=onDblClick() value="DOUBLE CLICK" height="20" width="20">    
  8. <h3>{{this.onDoubleClick}}</h3>    
Here from the above code, when user clicks or double clicks on a button on View, we are setting the value this.onButtonClick and this.onDoubleClick in onBtnClick() and
onDblClick() methods and displaying the result or message to the end user below the Click and Double click button.
 
The output can be displayed below.
Data Binding In Angular 8 
 

Two Way Data Binding

 
In Two-way data binding, the data can be bound from Component.html to Component.ts or Component.ts to Component.html (Viceversa).
 
If the textbox value in the View changes then Component.ts value also gets reflected, similarly if we change Component.ts value then the textbox value gets reflected.
 
In two way data binding we use [(ngModel)] attribute, as well as we, need to import FormsModule in app.Module.ts file like below.
  1. import {FormsModule} from '@angular/forms';    
and add FormsModule in imports array. 
  1. imports: [    
  2.     BrowserModule,    
  3.     AppRoutingModule,    
  4.     FormsModule    
  5.   ],    
 Open the Component.html file and place the below code.
  1. Two Way Binding: <input type="text" [(ngModel)]='this.fullName' value="{{this.fullName}}">    
  2. {{this.fullName}}    
With the above, if Textbox value changes then the Component.ts field this.FullName changes, as well as if we change Component.ts this.fullName with the new value it will be reflected in the TextBox.
 

Conclusion

 
In this article, we learned two different ways of binding the data in Angular.
 
Thanks & I hope this helps you.