當前位置:編程學習大全網 - 網站源碼 - Angular中文社區後臺返回的html代碼怎麽用ng控制

Angular中文社區後臺返回的html代碼怎麽用ng控制

在用angular作為前端搭建個人博客的時候,發現用AngularJS輸出html的時候,瀏覽器並不解析這些html標簽,不知道angularjs如何實現這種功能的。

但是這裏我們需要其顯示angular輸出的html能被瀏覽器解析怎麽辦呢?

通過api,發現通過指令 ng-bind-html來實現html的輸出。

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片

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

但是並不起作用,瀏覽器中顯示的還是html代碼。

‘後來發現還需要通過通過$sce服務來實現html的展示。

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片

angular.module("list",[]).controller("BlogListCtrl", BlogListCtrl).filter(

'to_trusted', ['$sce', function ($sce) {

return function (text) {

return $sce.trustAsHtml(text);

}

}]

)

這裏通過$sce構建壹個過濾器來對輸出的html進行過濾

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片

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

這樣就可以通過angularjs正常的輸出html標簽,並且被瀏覽器解析了

  • 上一篇:IIS腳本資源訪問是什麽意思?
  • 下一篇:如何配置局域網服務器,令同事能訪問我做的PHP網站。
  • copyright 2024編程學習大全網