angular
1.2以后(或更早?)移除了ng-bind-html-unsafe
,那么我要用这个directive来绑定html代码怎么办?随便一测试,它是不支持把html直接传给它的:
//html
<p ng-bind-html="m"></p>
//js
$scope.m="<b>text</b>";
//error:
[$sce:unsafe] Attempting to use an unsafe value in a safe context.
参考这篇文章,得到解决。我选择的是直接做一个过滤器:
//js
app.filter(‘to_trusted‘, [‘$sce‘, function ($sce) {
return function (text) {
return $sce.trustAsHtml(text);
};
}]);
app.controller...{
$scope.m="<b>text</b>"
}
//html:
<p ng-bind-html="m | to_trusted"></p>