Introduction
This article demonstrates how to configure various types of Binding in Angular. This article starts with an introduction to Databinding. After that, it demonstrates various types of Databinding with source-code explanation. In the end, the article discusses Attribute and Style Binding.
Databinding
Databinding is the process of coordinating or synchronizing the application values between the HTML elements and JavaScript or TypeScript objects. It’s a binder who sits between these two and binding it from all the ways. There are four different types of Databinding:
- Interpolation
- Property Binding
- Event Binding
- Two-way Binding
In this article, we will cover only the Interpolation and Property Binding. In the next article, we will see Event and Two-way binding.
Interpolation ( {{…. }} )
Component Property
The Component property is one way of Interpolation. In our case, EmployeeCurrentObj is an instance of EmployeeComponent class which has a property named EmployeeName. If we place the property inside the {{.. }}, Angular replaces that EmployeeName with string value of the Employee Component property.
{{EmployeeCurrentObj.EmployeeName}} <br />
Angular fetches the corresponding value from the EmployeeComponent and displays it in the View.
Template Expression
Look at the below source-code.
<img src="{{sImgURL}}" style="height:130px" /> <br />
sImgURL variable or property belongs to EmployeeComponent. Angular evaluates the value of sImgURL from the EmployeeComponent and fills it in the blanks {{.. }}, Value inside the {{..}} is a template expression that Angular evaluates and converts into a string. Even if you place a number data type inside the braces, it converts the number into a string and displays it.
Let’s see one more example of Template Expression
{{100 + 100}}
Do you think the above code will execute? Yes, it will. I have already told you that Angular evlauates the content inside the braces and converts it into a string and displays it. As part of the evaluation, Angular will add these two numbers and converts it into a string and displays it.
Even if you place a method inside the expression, Angular will execute the method and get the expression results, then it will be converted into a string and displays it.
{{100 + 100 +ProvideValue()}}
The code samples I have listed will also be in the attached source-code, please have a look on that and comment if you have other questions.
Expression Context
Let’s have a look at this source-code
<div *ngFor="let emp of Employees">{{emp.EmployeeName}}</div>
Actually, Expression context is a component instance. From the source-code, given above you can find that Expression {{..}} may also refer to the properties of the template context. emp.EmployeeName refers the template input variable emp. Template Input variables cannot refer everything in the namespace, They cannot refer a form or window, but they could refer only the properties of the template context
Statement Context
Have a look at this source-code,
<button *ngFor="let emp of Employees" (click)="AlertEmployee(emp)">{{emp.EmployeeName}}</button>
Statement context may also refer to the properties of a template. Statement context is a component instance, in our case, emp is the component instance. emp instance may also refer the properties of a own template context which is a click event in our case. emp instance can be passed to an event handling method of the component.
Property Binding

Join the conversation! Your thoughts help the community grow.