2017-02-14 101 views
0

我有这样一个模型JSON:角2复杂的属性绑定

[{ 
    "PropName":"disabled", 
    "Value":"false" 
}, 
{ 
    "PropName":"color", 
    "Value":"primary" 
}, 
{ 
    "PropName":"visible", 
    "Value":"false" 
}] 

现在我需要一些属性到按钮,在这种情况下,只有数据模型基色的第二个元素。

<button md-raised-button color="primary"></Button> 

PropName值应替换到颜色属性,而数据与价值初级值。

主要是我想动态地应用一个或多个属性,形成一个元素JSON数组与属性名称和值。有人能告诉我一个例子吗?

谢谢

+1

利用[ngStyle],会做的伎俩,我相信。 –

回答

0

在逻辑上,你将创建你的对象,并称之为例如元素。

然后在模板:

<button *ngFor="let element of elements" [color]="'primary': element.PropName == 'disabled'" [value]="element.Value" md-raised-button color="primary"></Button> 
+0

我不能使用ngClass,在这种情况下,我需要设置属性而不是css类,属性是color =“primary”,我正在使用角度材质2的按钮 – Stefano

+0

检查我在答案中做出的更改 – ZanattMan

+0

差不多......我有一个元素数组而不是单个元素模型,我需要一些东西来搜索正确的元素索引,其中property.name =“color” – Stefano

-1

我解决的话,但我不知道是不是最好的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; 

}