我的作用域中有一个字符串,我并不总是知道我是否希望HTML转义。本质上,我有一个布尔值,它将说明是否应该转义HTML。
代码
下面是我的一些示例代码:
$scope.result = "<b>foo</b>bar";
$scope.html = false; // In this case the HTML *would* be escaped这里有一个例子,HTML将像innerHTML一样插入
$scope.result = "<strike>foo</strike>bar";
$scope.html = true; // The HTML would be escaped我尝试过的其他解决方案
我不确定这样做的“角度方式”是什么,虽然我已经考虑过使用.removeAttribute的黑客,但我发现这是非常麻烦的,必须有一个更好的方法。
发布于 2016-01-02 04:48:57
例如:
<p ng-bind-html-unsafe="data.markupData"></p>使用ng-重复的迭代也很简单:
<div ng-repeat="item in items">
<p ng-bind-html-unsafe="item.markupData"></p>
</div>1.创建一个新的控制器
var ExampleCtrl = function($scope) {
}在HTML中:
<div ng-repeat="example in Examples" ng-controller="ExampleCtrl">
<p ng-bind-html="trustedExample"></p>
<hr />
</div>2.向控制器添加严格的上下文转义服务
var ExampleCtrl = function($scope, $sce) {
}3.对数据使用$sce.trustAsHtml函数
var ExampleCtrl = function($scope, $sce) {
$scope.trustedExample = $sce.trustAsHtml($scope.example);
}我希望这能帮到
https://stackoverflow.com/questions/34562609
复制相似问题