Data-binding means communication between the TypeScript code of your component and your template which the user sees. Suppose, you have some business logic in your component TypeScript code to fetch some dynamic data from the server and want to display this dynamic data to the user via template because the user sees only the template. Here, we need some kind of binding between your TypeScript code and template (View). This is where data-binding comes into the picture in Angular because it is responsible for this communication.
Data-binding can be either one-way or two-way. Angular provides various types of data binding -
- String Interpolation
- Property Binding
- Event Binding
- Two-way binding
You can download the complete project with data binding examples from here.
Let’s explore these bindings one by one.
String Interpolation is a one-way data-binding which is used to output the data from a TypeScript code to HTML template (view). It uses the template expression in double curly braces to display the data from the component to the view.
For Example – {{ data }}
I have already created an Angular project using Angular CLI (which is attached to this article). We will use this same project for all future articles.
app.component.ts
- import { Component } from '@angular/core';
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
- })
- export class AppComponent {
- title = 'Data binding using String Interpolation';
- }
In the app.component.ts file, we have a variable called “title” which has a string value. We want to display this title value on the app component HTML view to the user.
app.component.html
- <h2>
- {{ title }}
- </h2>
In the app.component.html file, we have used title variable for displaying title value. Now when this view will be rendered in the browser, title value will be displayed over the page.
Output
String Interpolation can resolve some other expressions. We have a few examples of it below –
Example 1
Let’s initialize two variables with two numbers and display their addition on the view.
- export class AppComponent {
- title = "Data binding using String Interpolation";
- numberA: number = 10;
- numberB: number = 20;
- }
In the View, the expression will look like below –
<p>Calculation is : {{ numberA + numberB }} </p>
Output
Example 2
Let's define a method in the TypeScript code and call it in the View.
- export class AppComponent {
- title = "Data binding using String Interpolation";
- numberA: number = 10;
- numberB: number = 20;
- addTwoNumbers() {
- return this.numberA + this.numberB;
- }
- }
Now, call this method in View and see the output.
<p>Addition is : {{ addTwoNumbers() }} </p>
Output
Property Binding
Property binding is also a one-way data binding, where we bind a property of a DOM element to a field which is a property we define in our component typescript code. Behind the scene, Angular converts string interpolation into property binding.
For Example – <img [src]=”imgUrl” />
However, we can use string interpolation here like <img src=”{{ imgUrl }}” /> , but property binding is always a lot cleaner and shorter syntax to bind image source.
My recommendation is when you want to simply display some dynamic data from a component on the view between headings like h1, h2, p etc, use string interpolation.
Suppose if you will use property binding for just displaying some value between h2 heading; the syntax will be –
<h2 [textContent]="title"></h2>
Here, we are binding the text content property of h2 DOM element with the title property that we defined in our component. You can see that this syntax obviously is longer than string interpolation one.
Note
String Interpolation and Property binding both are one-way binding. That means if field value in the component change, Angular will automatically update the DOM. But any changes in the DOM will not be reflected back in the component.
app.componnt.ts
Let’s define a property named “imgUrl” and bind this with an image tag in the template.
- export class AppComponent {
- title = "Data binding using Property Binding";
- imgUrl="https://avatars2.githubusercontent.com/u/20270535?s=40&v=4";
- }
app.component.html
Use this property in the View and see the output.
- <h2>{{ title }}</h2> <!-- String Interpolation -->
- <img [src]="imgUrl" /> <!-- Property Binding -->
Output
Event Binding
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 move etc. Let’s understand this with the help of an example –
Suppose we have a button in the HTML template and we want to handle the click event of this button. To implement event binding, we will bind click event of a button with a method of the component.
app.component.html
app.component.ts
- export class AppComponent {
- onSave(){
- console.log("Save operation is performed!");
- }
- }
Output
When a user clicks on this button, the button click event will fire and onSave method will be called, which will log a message in the console.
Access DOM event object using $event
Sometimes we need to access the event object that is raised in the event handler. For example on mouse move, we need to access the x and y positions using DOM event object, then we have to pass $event as a parameter. Let’s understand this with the help of an example –
app.component.html

Former memberPosted Oct 29, 2019, 1:09 AM
Very well explained!!!!
Tsuriel AshurovPosted May 6, 2019, 7:38 AM
Thank you, very clear and extensive article.
Alberto GarciaPosted Oct 17, 2018, 8:38 AM
Hay unas variantes del 'binding' que no conoc?a, me fue de mucha utilidad el articulo.
Deepak DwivediPosted Oct 4, 2018, 7:25 AM
Very useful article