我是 Angular2 的新手。当 iam 绑定属性时,我通常按以下方式进行操作。
示例1:
<input type="number" [max]="variableName">
示例2:
<select [(ngModel)]="selectedItem">
<option *ngFor="let item of itemList" [value]="item.value" [selected]="selectedItem==item.value">{{item.name}}</option>
</select>
有时这些绑定会失败。
然后我使用以下语法通过后缀 attr 来绑定属性。为了它。
示例1:
<input type="number" [attr.max]="variableName">
示例2:
<select [(ngModel)]="selectedItem">
<option *ngFor="let item of itemList" [value]="item.value" [attr.selected]="selectedItem==item.value">{{item.name}}</option>
</select>
这些语法有时非常有效。
请帮助我了解这两种绑定之间的区别[attributename]
and [attr.attributeName]
使用这些特定语法非常重要。
这是属性绑定
[selected]="selectedItem==item.value"
这是属性绑定
[attr.selected]="selectedItem==item.value"
也可以看看属性和属性有什么区别? https://stackoverflow.com/questions/258469/what-is-the-difference-between-attribute-and-property为了区别property和attribute。
仅当元素实际上具有具有该名称的属性时,属性绑定才起作用。某些属性会自动反映到属性中。
Attribute 只是将具有该名称的属性添加到 DOM 元素。已知属性由元素读取(并且@Input()
角度组件)。
自定义属性仅添加到 DOM,而不由元素处理。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)