angular输出html

来源:互联网 发布:大麻淘宝 编辑:程序博客网 时间:2024/05/22 05:33

在用angular作为前端搭建个人博客的时候,发现用angularJs输出html的时候,浏览器并不解析这些html标签,不知道angularjs如何实现这种功能的。

但是这里我们需要其显示angular输出的html能被浏览器解析怎么办呢?

通过api,发现通过指令 ng-bind-html来实现html的输出。

<div class="col-md-12 ng-binding" ng-bind-html="item.content ">

但是并不起作用,浏览器中显示的还是html代码。

‘后来发现还需要通过通过$sce服务来实现html的展示。

angular.module("list",[]).controller("BlogListCtrl", BlogListCtrl).filter('to_trusted', ['$sce', function ($sce) {return function (text) {    return $sce.trustAsHtml(text);    }}])

这里通过$sce构建一个过滤器来对输出的html进行过滤

<div class="col-md-12 ng-binding" ng-bind-html="item.content|to_trusted ">
这样就可以通过angularjs正常的输出html标签,并且被浏览器解析了

1 0
原创粉丝点击