问题
How to change the color of a div in code dynamically in ionic by using code?
回答1:
If it's just a single style, you can use property binding for this. In your component:
public aColor: string = "#ccc";
And then in the view:
<div [style.background-color]="aColor"></div>
Please take a look at the ngStyle docs for more info if you need to set more than one style dynamically:
<some-element [ngStyle]="{'font-style': styleExp}">...</some-element>
<some-element [ngStyle]="{'max-width.px': widthExp}">...</some-element>
<some-element [ngStyle]="objExp">...</some-element>
来源:https://stackoverflow.com/questions/45115813/ionic-change-background-color-of-div-by-code