角2复杂的属性绑定
问题描述:
我有这样一个模型JSON:角2复杂的属性绑定
[{
"PropName":"disabled",
"Value":"false"
},
{
"PropName":"color",
"Value":"primary"
},
{
"PropName":"visible",
"Value":"false"
}]
现在我需要一些属性到按钮,在这种情况下,只有数据模型基色的第二个元素。
<button md-raised-button color="primary"></Button>
PropName
值应替换到颜色属性,而数据与价值初级值。
主要是我想动态地应用一个或多个属性,形成一个元素JSON数组与属性名称和值。有人能告诉我一个例子吗?
谢谢
答
在逻辑上,你将创建你的对象,并称之为例如元素。
然后在模板:
<button *ngFor="let element of elements" [color]="'primary': element.PropName == 'disabled'" [value]="element.Value" md-raised-button color="primary"></Button>
答
我解决的话,但我不知道是不是最好的pratics:
<button md-raised-button [color]="getColorButton()">
{{dati.Label}}
</button>
从TS组件,我从模型和返回值 搜索颜色属性getColorButton():string { if(this.colore!= null)return this.colore;
this.dati.Proprieta.forEach(element => {
if (element.Nome=='color'){
this.colore = element.Valore;
}
});
return this.colore;
}
利用[ngStyle],会做的伎俩,我相信。 –