将style属性应用于angular js中的ng-bind指令

Sha*_*dse 2 angularjs angularjs-directive

我们使用角度js中的ng-bind指令创建了div.我们无法将样式属性应用于同一个div

<div ng-bind-html="test"></div>
Run Code Online (Sandbox Code Playgroud)

在控制器文件中我将测试变量定义为

$scope.test="<div style="background:red;"></div>
Run Code Online (Sandbox Code Playgroud)

我们需要应用style属性.使用CSS属性,我能够做到.但每次背景都需要改变.

mic*_*ael 5

可能是你想要的wat是这样的:

<div ng-style="{'background-color': bgColor}"></div>
Run Code Online (Sandbox Code Playgroud)

在你的控制器中

$scope.bgColor = "red";
Run Code Online (Sandbox Code Playgroud)