Open In App

Why is colspan not a known native attribute in Angular 2?

Improve
Improve
Like Article
Like
Save
Share
Report

In order to understand it, we need to have a clear in-depth knowledge between Document Object Model(DOM) and HyperText Markup Language(HTML).
Basically, DOM is a model of objects and is a logical tree representation of the document whereas HTML is a markup language that we used to build web-pages.
Every HTML attributes have one to one mapping to properties of DOM objects.

Example: First Name




<label for="fname">First Name </label>
<input type="text" id="fname" name="fname" disabled >


So, The above example contains two tags label and input. As we can observe, there is an attribute defined inside the input tag which is “disabled”.
Similarly, if you talk about DOM properties, disabled is also present inside the input object.
But they are cases where some attributes of HTML tags are not available inside the DOM. 
Example: colspan. Like colspan, So when you try to use them for attribute binding in Angular 2+, you will get an error which says “can’t bind to colspan since it isn’t a known property of ‘td’ “.
Similarly, we have some properties which are defined in DOM but are not present in HTML.

Example:




<h2 [textContent]="title"></h1>


Solution:
In order to use them for attribute binding you need to use the following syntax “[attr.colspan]”.




import {Component} from '@angular/core';
  
  
@Component({
  
selector:'app-colspan',
template:
`
<h4> Colspan Usage </h4>
<table>
  <tr>
   <td [attr.colspan]="colSpan">First Item </td>
  </tr>
  <tr>
   <td>Second Item</td>
   <td>Third Item </td>
  </tr>
</table>
`,
styles:[`
  
table, tr, td{
border: 1px solid black;
border-collapse:collapse;
}
`]
})
  
  
export class ColSpanComponent{
  
colSpan="2"
  
}


Output:



Last Updated : 26 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads