Data Binding in Angular means bind the data in component’s HTML file from type script file. We can get and set DOM values dynamically using Data Binding.
Data binding in Angular is either one-way or two-way.
Types of Binding available in Angular-
- Interpolation
- Property Binding
- Two-Way Binding
- Event Binding
Interpolation
Suppose, we have some data available in component class file which we got via some API.
Read:- Angular Interview Questions
Now, we have to present this data on HTML. That’s the usual task we do with the data.
To display all values –
If you want to print the available values one by one
Above expression will print 50 on screen.
Property Binding
It is a one-way binding used to bind a property of DOM element.
Component class file –
Component HTML file –
One major example for property binding is
<img [src]=”imgURL”/>
Example –
Component class file-
Component HTML file-
Two-way Binding
Any changes made in view will reflect in component typescript file.
ngModel directives is used to implement two-way data binding in Angular.
Syntax to implement two-way binding in Angular –
[(ngModel)]=”Value”
Component class file –
Component HTML file –
Event Binding
Event Binding in Angular is used to handle the events from HTML page. For eg – Button Click.
Component class file –
Component HTML file-
In case we want to use parameter in event binding, follow below sample code.
Component class file –
Component HTML file –
Please share this blog on social media and give your feedback in comment box below.
You may like other blogs –
Interview Questions and Answers Series –
MVC Interview Questions and Answers
Web API interview questions and answers
Keep following – SharePointCafe.Net
Prev – Angular CRUD Example with ASP.Net Web API
Next – Route Guards in Angular