angular2 标签中attribute和property

原链接:http://blog.csdn.net/erciyuan_nuonuo/article/details/60971696

property:dom元素作为对象附加的内容,例如childNodes、firstChild等 
attribute:HTML标签特性是dom节点自带的属性

异同: 
1 . 部分属性既属于property,又属于attribute,比如id 
2 . attribute初始化后不会再改变;property默认值为初始值,会随着dom更新

所以在angular2中双向绑定实现是由dom的property实现的,所以指令绑定的是property,但是在某些情况下dom不存在某个property比如colspan,rowspan等,这时想要绑定html标签特性需要用到attr

<table width="100%" border="10px solid">
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td [attr.colspan]=colnum>January</td>
  </tr>
  <tr>
    <td [attr.colspan]=colnum>February</td>
  </tr>
</table>

let colnum:number = 2;
posted @ 2017-09-16 18:37  lyls  阅读(176)  评论(0编辑  收藏  举报