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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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"
  
}

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.