In this Video, I am going to discuss Angular Property Binding with examples. Please watch our previous Video where we discussed Angular Interpolation with some different kinds of examples. In our Angular Interpolation Video, we discussed that we can use the Angular interpolation technique to bind the component class properties to the view template. We can also achieve the same thing by using the Property binding technique. So, at the end of this Video, you will understand the following pointers.
- What is Angular Property Binding?
- How to use Property Binding in Angular Application?
- Different kinds of examples to understand Property Binding.
- What are the differences between Property Binding and Interpolation?
- Working with non string data.
- Angular Interpolation with Boolean data
- Providing Security to Malicious Content
What is Angular Property Binding?
The Property Binding in Angular Application is used to bind the values of component or model properties to the HTML element. Depending on the values, it will change the existing behavior of the HTML element. The syntax to use property is: [property] = "expression"
In Property Binding, there is a source and target. For example, consider the below span tag.
span[innerHTML] = "FirstName".
Here, innerHTML is the target that is a property of span tag and FirstName is the source that is the component property.
How to use the Property Binding in Angular Application?
In order to understand how to use property binding in the angular application, please have a look at the following image. As you can see in the below image, you need to specify the HTML element property (in our example, the HTML element is span and the property is innerHTML) in a pair of the square brackets. Then you need to specify the component class property (in our example, the component class is AppComponent and the property is Title) in a pair of single quotes. Then at runtime, this property value will be assigned to the HTML Property.
So, please modify the app.component.ts file as shown below to use the property binding to bind the component property values to the HTML elements property.
Now, save the changes and have a look at the browser and you should get the output as expected as shown in the below image.
In this way, you can use property binding in angular application.
Achieving the same thing using Angular Interpolation and Property Binding:
At the start of this Video, we had made one point that we can achieve the same thing using property binding as we achieve using Angular Interpolation. In our previous Video, we use the following code to bind component class ImagePath property to the <img> elements src property using the interpolation technique.
Now, lets see how we can achieve the same thing using Property Binding. With property binding what you need to do is, you need to specify the src property of the image element in a pair of the square brackets and then you need to specify the ImagePath property of the component class in a pair of the single quotes as shown in the below image.
So, modify the app.component.ts file as shown below and see the output in the browser which should the image as expected.
Note: The Angular Interpolation and Property binding both flows the values from a component to the view template i.e. in one direction.
Now, you may have one question i.e. we can achieve the same thing using both property binding and angular interpolation, then what is the difference between them or when to use one over another.
What is the difference between Property Binding and Angular Interpolation?
Interpolation in Angular is just an alternative approach for property binding. It is a special type of syntax that converts into a property binding.
But there are some scenarios where we need to use interpolation instead of property binding. For example, if you want to concatenate strings then you need to use angular interpolation instead of property binding as shown in the below image.
Working with non-string (Boolean) data:
When you are working with non-string values like Boolean to set the property value of an HTML element, then you need to use property binding instead of interpolation. For example, if you want to disable a button click event, then you need to set the disabled property of the button element using the property binding as shown in the below code.
With the above changes in place, it will display the button in the browser which is not clickable. Let see what happens if we use angular interpolation.
Angular Interpolation with Boolean data:
Please modify the app.component.ts file as shown below to use angular interpolation to set the disabled property of the button element. Here, we set the property value to false.
With the above changes in place, irrespective of the IsDisabledClick property value of the component class, the button is always disabled. Here we set the IsDisabled property value as false but when you run the application, it will not allow the button to be clickable.
Providing Security to Malicious Content:
From the security point of view, both Angular data binding and Angular Interpolation protect us from malicious HTML content before rendering it on the web browser. Let us understand this with an example. In the following example, we are using angular interpolation to bind the malicious <script> tag.
Now, when we run the application, the Angular interpolation sanitizes the malicious content and displays the following in the browser.
In the below example we are using property binding.
The Property Binding handles the malicious HTML content in a slightly different manner and when you run the application, you will get the following output in the browser. But the most important point that you need to keep in mind is, both property binding and interpolation protect us from malicious HTML content.