您的位置:首页 > Web前端 > AngularJS

关闭Angularjs对HTML标签自动转义

2017-05-16 11:41 369 查看
当Angular绑定的字符串中含有HTML标签的时候,其中碰到 AngularJS 获取的是一段 HTML 文本,如果直接使用 data-ng-bind 的话是被转义过的,使用 data-ng-bind-html 则可以取消转义。但是直接使用
data-ng-bind-html 的话会提示错误。

Error: [$sce:unsafe] Attempting to use an unsafe value in a safe context.
HTML 片段需要先使用 $sce.trustAsHtml(html_in_string) 将标记为信任,然后才可以使用 data-ng-bind-html="html_in_string" 取消转义。
angular.module('myApp',[])
.controller('orderDetailController',function($scope,$http,$location,$timeout,$sce){
AngularAction($scope,$http,$location,$timeout,$sce);
});
function AngularAction($scope,$http,$location,$timeout,$sce){
$scope.comment = $sce.trustAsHtml($scope.pricelist.comment);
}



HTML代码如下

<p style="line-height:2;display: inline">
<span data-ng-bind-html ="comment"></span>
</p>



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  angular 转义