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

Angularjs输出html

2016-05-10 22:26 671 查看
第一种方式 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);
  };
}])


只需要改变$scope.content的值为要输出的html就可以输出被浏览器解析的html代码,但是如果代码中有ng-model或者ng-click这种绑定的方法和对象,not-working

这时候改用第二种方式:compile

html

<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);
}
)};
}])


运行一下,完美的解决了问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: