1、内部域直接信任
<div ng-bind-html="preview_data.preview.embed.htmlSafe"></div>
myApp.controller('myCtrl', ['$scope', '$sce', function($scope, $sce) {
$scope.preview_data.preview.embed.htmlSafe =
$sce.trustAsHtml(preview_data.preview.embed.html);
}
2、内部域信任函数
<div ng-bind-html="to_trusted(preview_data.preview.embed.html)"></div>
$scope.to_trusted = function(html_code) {
return $sce.trustAsHtml(html_code);
}
3、信任过滤器
<div ng-bind-html="preview_data.preview.embed.html | to_trusted"></div>
angular.module('myApp')
.filter('to_trusted', ['$sce', function($sce){
return function(text) {
return $sce.trustAsHtml(text);
};
}]);
本文介绍了在AngularJS应用中如何使用内置的信任机制安全地绑定HTML内容,包括直接信任、信任函数及信任过滤器三种方法。
371

被折叠的 条评论
为什么被折叠?



