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

后端 未结 3 755
情深已故
情深已故 2020-12-08 03:33

If we attempt code like this:

Column

or this:

Colu         


        
3条回答
  •  我在风中等你
    2020-12-08 04:22

    **Similar example

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

    In Angular

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

    or

    but

    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)

    Column
    

    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.

提交回复
热议问题