当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>