angular2 標籤中attribute和property
阿新 • • 發佈:2019-01-27
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;
Month | Savings |
---|---|
January | |
February |