第一种方式 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);
}
)};
}])
运行一下,完美的解决了问题。