码迷,mamicode.com
首页 > Web开发 > 详细

【AngularJs】---$sce 输出Html

时间:2015-04-10 19:47:26      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

【问题描述】

angular js的强大之处之一就是他的数据双向绑定功能----->ng-bind和针对form的ng-model

但在我们的项目当中会遇到这样的情况,后台返回的数据中带有各种各样的html标签

angularJs输出html的时候,浏览器并不解析这些html标签

我们用ng-bind-html这样的指令来绑定,浏览器中显示的还是html代码

【解决办法---$sce】

通过使用$ sce.trustAsHtml()。该方法将值转换为特权所接受并能安全地使用“ng-bind-html”

咱们还可以这样用,把它封装成一个过滤器就可以在模板上随时调用了

filter code:

app.filter(‘to_trusted‘, [‘$sce‘, function ($sce) {
  return function (text) {
      return $sce.trustAsHtml(text);
  };
}]);

html code:

<p ng-bind-html="currentWork.description | to_trusted"></p>

 

【AngularJs】---$sce 输出Html

标签:

原文地址:http://www.cnblogs.com/xuan-0107/p/4415271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!