Angularjs 将style属性应用于angular js中的ng bind指令
我们在angular js中使用ng bind指令创建了div。我们无法将样式属性应用于同一个divAngularjs 将style属性应用于angular js中的ng bind指令,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我们在angular js中使用ng bind指令创建了div。我们无法将样式属性应用于同一个div <div ng-bind-html="test"></div> 在contrller文件中,我将测试变量定义为 $scope.test="<div style="background:red;"></div> $scope.test=” 我们需要应用style属性。使用CSS属性我可以做到。但是每次需要更改背景时。可能是您想要的是: &l
<div ng-bind-html="test"></div>
在contrller文件中,我将测试变量定义为
$scope.test="<div style="background:red;"></div>
$scope.test=”
我们需要应用style属性。使用CSS属性我可以做到。但是每次需要更改背景时。可能是您想要的是:
<div ng-style="{'background-color': bgColor}"></div>
我不知道您想要什么,但您的代码唯一的错误是您使用的是angular<1.2版本 ng bind html通过服务运行代码,该服务检查不安全代码。在您的示例中,I和“样式”将被删除。不安全版本不会执行此检查,因此,如果您在无法完全控制的某些情况下使用它(例如,所见即所得编辑器保存到数据库并显示注释)这些数据可以让朋克四处运行,执行恶意脚本 小提琴示例: 支持代码
<div ng-app>
<div ng-controller="Ctrl">
<div id="nick" ng-bind-html-unsafe="test"></div>
</div>
</div>
--js
function Ctrl($scope) {
$scope.test='<div id="child" style="background:red;"></div>'
}
--css
#nick{
width: 300px;
height: 300px;
}
#child{
width: 200px;
height: 200px;
}
--js
函数Ctrl($scope){
$scope.test=''
}
--css
#尼克{
宽度:300px;
高度:300px;
}
#孩子{
宽度:200px;
高度:200px;
}
你能给我们看一把小提琴吗(angular..angular..的哪个版本和旧版本?在控制器中修复你的报价,然后再试一次。你能告诉我不安全和ng bind html的区别吗。@Shardupendse刚刚更新。
<div ng-app>
<div ng-controller="Ctrl">
<div id="nick" ng-bind-html-unsafe="test"></div>
</div>
</div>
--js
function Ctrl($scope) {
$scope.test='<div id="child" style="background:red;"></div>'
}
--css
#nick{
width: 300px;
height: 300px;
}
#child{
width: 200px;
height: 200px;
}