Angularjs输出html
发布时间:2020-12-17 10:10:23 所属栏目:安全 来源:网络整理
导读:第一种方式 ng-bind-html html div id = "popup" ng-bind-html= "popup.content | to_trusted" / div angularjs app.filter( 'to_trusted' ,[ '$sce' , function ( $sce ) { return function (text) { return $sce .trustAsHtml(text); };}]) 只需要改变$sco
第一种方式 ng-bind-html <div id="popup" ng-bind-html="popup.content | to_trusted"></div>
angularjs app.filter('to_trusted',['$sce',function ($sce) {
return function (text) {
return $sce.trustAsHtml(text);
};
}])
只需要改变$scope.content的值为要输出的html就可以输出被浏览器解析的html代码,但是如果代码中有ng-model或者ng-click这种绑定的方法和对象,not-working <div id="popup" compile="popup.content"></div>
angularjs app.directive('compile',['$compile',function ($compile) {
return function(scope,element,attrs) {
scope.$watch(
function(scope) {
return scope.$eval(attrs.compile);
},function(value) {
element.html(value);
$compile(element.contents())(scope);
}
)};
}])
运行一下,完美的解决了问题。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |