方案一.
html:
<div ng-repeat="article in articles">
<div class="panel-heading">
<h4><b>{{article.title}}</b></h4>
</div>
<div class="panel-body">
<article id="word-display" ng-bind-html="article.content | trustHtml">
</article>
</div>
</div>
javascript:
success(function (data) {
$scope.articles = data;
});
myApp.filter('trustHtml', function ($sce) {
return function (input) {
return $sce.trustAsHtml(input);
}
});
其中$sce是angularJS自带的安全处理模块,$sce.trustAsHtml(input)方法便是将数据内容以html的形式进行解析并返回。将此过滤器添加到ng-bind-html所绑定的数据中,便实现了在数据加载时对于html标签的自动转义。
方案二:
我们给模块添加依赖 'ngSanitize' ,
var app =angular.module('myApp',['ngSanitize']);(需要链入 angular-sanitize.min.js ).然后使用ng-bind-html,$sanitize会自动对ng-bind-html绑定的值进行净化.
*记住,$sanitize指令本身不会出现在js代码里.直接使用ng-bind-html就行了.但如果这里不给模块添加依赖ngSanitize,是会报错的.
angularjs干货地址 : http://blog.csdn.net/column/details/angularjs-sunny1989.html