Angular 2 复杂 属性 绑定
Angular 2 complex property binding
我有一个这样的模型json:
[{
"PropName":"disabled",
"Value":"false"
},
{
"PropName":"color",
"Value":"primary"
},
{
"PropName":"visible",
"Value":"false"
}]
现在我需要将几个 属性 应用到一个按钮,在本例中只需要数据模型颜色主色的第二个元素。
<button md-raised-button color="primary"></Button>
PropName
值应替换为 color 属性,而数据值具有 primary 值。
主要是想动态应用一个或多个属性,形成一个元素json的数组,名称和值属性。
有人可以给我举个例子吗?
谢谢
根据逻辑,您将创建对象并调用它作为示例元素。
然后在模板中:
<button *ngFor="let element of elements" [color]="'primary': element.PropName == 'disabled'" [value]="element.Value" md-raised-button color="primary"></Button>
我这样解决了,但我不知道是否最好实践:
<button md-raised-button [color]="getColorButton()">
{{dati.Label}}
</button>
从 ts 组件中我从模型中搜索颜色 属性 和 return 值
getColorButton():字符串{
如果 (this.colore!=null) return this.colore;
this.dati.Proprieta.forEach(element => {
if (element.Nome=='color'){
this.colore = element.Valore;
}
});
return this.colore;
}
我有一个这样的模型json:
[{
"PropName":"disabled",
"Value":"false"
},
{
"PropName":"color",
"Value":"primary"
},
{
"PropName":"visible",
"Value":"false"
}]
现在我需要将几个 属性 应用到一个按钮,在本例中只需要数据模型颜色主色的第二个元素。
<button md-raised-button color="primary"></Button>
PropName
值应替换为 color 属性,而数据值具有 primary 值。
主要是想动态应用一个或多个属性,形成一个元素json的数组,名称和值属性。 有人可以给我举个例子吗?
谢谢
根据逻辑,您将创建对象并调用它作为示例元素。
然后在模板中:
<button *ngFor="let element of elements" [color]="'primary': element.PropName == 'disabled'" [value]="element.Value" md-raised-button color="primary"></Button>
我这样解决了,但我不知道是否最好实践:
<button md-raised-button [color]="getColorButton()">
{{dati.Label}}
</button>
从 ts 组件中我从模型中搜索颜色 属性 和 return 值 getColorButton():字符串{ 如果 (this.colore!=null) return this.colore;
this.dati.Proprieta.forEach(element => {
if (element.Nome=='color'){
this.colore = element.Valore;
}
});
return this.colore;
}