In this Video, I am going to discuss the Angular Attribute Binding with examples. Please watch our previous Video where we discussed HTML Attribute vs DOM Property in Detail. At the end of this Video, you will understand the following pointers in detail.
- What is Attribute Binding in Angular?
- Why we need Attribute Binding in Angular Application?
- Understanding Angular Attribute Binding with an Example
- Using Angular Interpolation
- Using Property Binding and Attribute Binding in Angular
Why we need Attribute Binding in Angular Application?
we discussed that they both (Interpolation and Property Binding) are dealing with the DOM Properties but not with the HTML attributes. But there are some HTML elements (such as colspan, area, etc) that do not have the DOM Properties. Now the question is how to deal with such elements that do not have DOM Properties as we cant use Interpolation and Attribute Binding. The Answer is Angular Attribute Binding.
With Attribute Binding in Angular, you can set the value of an HTML Element Attribute directly. So, the Attribute Binding is used to bind the attribute of an element with the properties of a component dynamically.
Understanding Angular Attribute Binding with an Example:
Let us understand the need and use of Angular Attribute Binding with an example. We want to do here is, we want to show the Details of a Student on a webpage as shown in the below image.
Let us see how to achieve this step by step.
Step1: Modify the app.component.css file
First, open app.component.css file which you can find inside the app folder and then copy and paste the following styles into it. These are the styles that we want to apply to the table.
Step2: Modify the app.component.html file
Open app.component.html file and then copy and paste the following code in it. You can also find this file inside the app folder. Here, we have created one table to display the student details. Again here we are using angular interpolation i.e. double curly braces to bind the student data.
Step3: Modify the app.component.ts file
Modify the app.component.ts file as shown below. Here within the component decorator, we are providing the stylesUrl and templateUrl values to the external files that we just modified in our previous two steps. Then as part of the Component class, we are creating the required student properties with some value as well as the header value.
Now, with the above changes in place, if you browse the application (ng serve -o), you will get the output as expected.
Using Angular Interpolation:
If you notice, here we have hardcode the colspan value to 2 in the HTML file i.e. in the app.component.html file. Let us say we also want to bind this (colspan) value from a property of the component class. To do so, let us add a new property with the name ColumnSpan in the AppComponent (app.component.ts file) class as shown in the below image.
Now, you may be intended to use the ColumnSpan property to set the colspan attribute of the th element using angular interpolation as shown in the below image.
Once you compiled, you will get the following error.
Now, have a look at the browser, and you will not get the output as expected as shown in the below image. The colspan is not working here.
In order to see what went wrong, launch the browser developer tools by pressing the F12 key and open the Console tab as shown in the below image.
It clearly shows that you “Cant bind to "colspan since it isnt a known property of "th"”. So, you cannot use angular interpolation to bind the colspan attribute of th element.
Using Property Binding in Angular:
You will also get the same error if you try to bind the colspan property using the Property Binding Technique. Let us prove this. First, modify the colspan binding in the app.component.html file as shown below.
With the above Property Binding in place, now if you compile the project, then you will get the same error as shown in the below image.
We get the above error. The reason for this error is, we do not have a corresponding property in the DOM for the colspan attribute. In order to solve the above error, what you need to do is you need to use Angular Attribute Binding, which will set the colspan attribute value.
Using Attribute Binding in Angular:
To tell the angular framework that we are setting an attribute value, we have to prefix the attribute name with the attr and a DOT as shown below.
The same is also true when we are using angular interpolation as shown in the below image.
Now run the application and you should get the output as expected.
Note: The Angular team recommends using the property binding or Interpolation whenever possible and use the attribute binding only when there is no corresponding element property to bind.