Angular5 - 动态设置div容器的颜色

qum*_*uma 1 angular angular5

使用AngularJS,这很好用:

<div style="width:10px;height:10px;background-color:{{user.color}}"></div>
Run Code Online (Sandbox Code Playgroud)

但是对于Angular5,它没有.如何使用Angular5?

Veg*_*ega 10

<div style="width:10px;height:10px;" [ngStyle]="{'background-color': user.color}">...</div>
Run Code Online (Sandbox Code Playgroud)

要么

<div [style.background-color]="user.color">...</div>
Run Code Online (Sandbox Code Playgroud)

文件

如何使用ngStyle

<some-element [ngStyle]="{'font-style': styleExp}">...</some-element>

<some-element [ngStyle]="{'max-width.px': widthExp}">...</some-element>

<some-element [ngStyle]="objExp">...</some-element>
Run Code Online (Sandbox Code Playgroud)