Data binding is a mechanism in Angular that allows you to connect the values of an Angular component with the values of an HTML template. It provides a way to keep the component state and the template in sync, so that when the component’s data changes, the template automatically updates, and vice versa.
There are three main types of data binding in Angular:
Interpolation Binding:
This is the most basic type of data binding in Angular, which allows you to bind the value of a component property to an element in the HTML template. The syntax for interpolation binding is {{expression}}
. For example:
In this example, the value of the message
property is displayed within the <p>
element in the HTML template.
Property Binding:
This type of data binding allows you to bind the value of a component property to a property of an HTML element. The syntax for property binding is [property]="expression"
. For example:
In this example, the value of the message
property is bound to the value
property of the <input>
element in the HTML template.
Event Binding:
This type of data binding allows you to bind the value of a component property to an event of an HTML element. The syntax for event binding is (event)="expression"
. For example
In this example, the onClick
method of the component is bound to the click
event of the <button>
element in the HTML template. When the button is clicked, the onClick
method is executed and the component's state can be updated.
These are the three main types of data binding in Angular, but there are other forms of data binding as well, such as two-way binding, which combines property binding and event binding to automatically update the component state and the template whenever either changes.