如何通过角度绑定值设置背景色

时间:2018-06-21 07:59:06

标签: css angular

我有一个简单的角度应用程序(版本4.2.6),用于显示图块。我想通过绑定到图块对象的值设置这些图块的背景色。设置图块的内容按预期工作时,设置颜色失败:

<div *ngFor="let tile of tiles" class="col-md-3 col-sm-6 col-xs-12" >
    <div style="border-style: solid;width: 150px;height: 150px;background-color:{{tile.color}};" class="tile">{{tile.name}}</div>
</div>

如何通过绑定值tile.color设置背景颜色(背景颜色为十六进制字符串)?

2 个答案:

答案 0 :(得分:2)

您可以使用[style.backgroundColor]="tile.color"

 <div [style.backgroundColor]="tile.color" ...></div>

答案 1 :(得分:1)

使用ngStyle指令

<div [ngStyle]="{'background-color':tile.color}" style="border-style: solid;width: 150px;height: 150px;" class="tile">{{tile.name}}</div>