Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

314
Visualizações
Why is colspan not a known native attribute in Angular 2?

If we attempt code like this:

<td [colspan]="1 + 1">Column</td>

or this:

<td colspan="{{1 + 1}}">Column</td>

We soon find out that "colspan is not a known native attribute."

From the A2 docs we learn that:

the element does not have a colspan property. It has the "colspan" attribute, but interpolation and property binding can set only properties, not attributes.

We must instead do this:

<td [attr.colspan]="1 + 1">Column</td>

Which is fair enough.

Question:

My question is, why is colspan not an attribute of the DOM, and if it is missing, how can the browser possibly render tables, since the browser renders the DOM and not the HTML?

Also, if I open my Chrome inspector, and go to the properties tab, why can I see colspan as a property of the Element?

Why does the DOM exhibit this inconsistency?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

**Similar example <label for=...>

Property and attribute aren't always 1:1. An often encountered example is the label tag

<label for="someId">

In Angular

<label [for]="someId"> 

fails with the same error and you'd need to bind like

<label attr.for="{{someId}}">

or

<label [attr.for]="someId">

but

<label [htmlFor]="someId">

would also work because in this case htmlFor is the property that is reflected to the DOM for attribute. See also https://developer.mozilla.org/de/docs/Web/API/HTMLLabelElement for the htmlFor property (in the Properties section)

See also What is the difference between attribute and property?

colSpan the actual property name

According to https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableCellElement colSpan is the property that is reflected to the colspan attribute therefore (uppercase S)

<td [colSpan]="1 + 1">Column</td>

See also https://plnkr.co/edit/BZelYOraELdprw5GMKPr?p=preview

works just fine.

Why does Angular bind to properties by default

Angular binds to the property by default for performance reasons. Binding to an attribute is expensive because attributes are reflected in the DOM and a change in the DOM can causes reevaluation of CSS styles that might match after the change, while properties are just a value in a JavaScript object that changed.
With attr. you opt in explicitely to the expensive behavior.

over 4 years ago · Santiago Trujillo Relatório

0

My question is, why is colspan not an attribute of the DOM, and if it is missing, how can the browser possibly render tables, since the browser renders the DOM and not the HTML?

Colspan is an attribute of the DOM but it's not a property, so it's read only and browser renders it because it's an attribute.

Also, if I open my Chrome inspector, and go to the properties tab, why can I see colspan as a property of the Element?

The chrome shows both attributes and properties when you inspect it.

If you consider following,

<html>
  <head>
  </head>
  <body>
  <table>
    <tr><th>A</th><th>A</th></tr>
    <tr><td colspan="2" id="xyz">B</td></tr>
  </table>
  </body>
</html>

document.getElementById('xyz').colspan results in undefined Since it's not a property

but document.getElementById('xyz').id results in xyz Since it's a property

over 4 years ago · Santiago Trujillo Relatório

0

Attributes & properties in Angular:

When the browser parses the HTML it will create an in memory DOM representation of the parsed HTML. What the data from the attributes often will become initial values for properties which are present in the DOM.

Since colspan isn't a DOM property of an <td> but colSpan (capital letter S) is one you will have to use the colSpan property. Here is an <td> element shown in chrome devtools:

enter image description here

We can see that the html attributes are saved in the attribute DOM property. It is important to realise that the current colspan is reflecting in the DOM colSpan property which can be observed below in the image.

When you are using property binding in Angular you are binding literaly 1 to 1 with these DOM properties. So in order to bind to the colSpan property we would need the following syntax:

<td [colSpan]="1 + 1">Column</td>

We can also bind directly to attributes in Angular, as you pointed out with the following syntax:

<td [attr.colspan]="1 + 1">Column</td>

Why does the DOM exhibit this inconsistency?

  1. For consistency reasons all DOM properties are lower camel cased
  2. Not all attributes can be transformed into DOM properties in a 1 to 1 fashion. Take for example the class attribute, we can see in the example image that the class attribute in our HTML results in 2 DOM properties (classList, className).
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda