【问题描述】

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>

  

相关文章:

  • 2021-06-03
  • 2022-12-23
  • 2022-12-23
  • 2022-01-26
  • 2022-12-23
  • 2021-09-27
猜你喜欢
  • 2022-12-23
  • 2022-02-24
  • 2021-08-03
  • 2022-12-23
  • 2022-01-12
  • 2022-12-23
相关资源
相似解决方案