加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angularjs输出html

发布时间:2020-12-17 10:10:23 所属栏目:安全 来源:网络整理
导读:第一种方式 ng-bind-html html div id = "popup" ng-bind-html= "popup.content | to_trusted" / div angularjs app.filter( 'to_trusted' ,[ '$sce' , function ( $sce ) { return function (text) { return $sce .trustAsHtml(text); };}]) 只需要改变$sco

第一种方式 ng-bind-html
html

<div id="popup" ng-bind-html="popup.content | to_trusted"></div>

angularjs

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

只需要改变$scope.content的值为要输出的html就可以输出被浏览器解析的html代码,但是如果代码中有ng-model或者ng-click这种绑定的方法和对象,not-working
这时候改用第二种方式:compile
html

<div id="popup" compile="popup.content"></div>

angularjs

app.directive('compile',['$compile',function ($compile) {
  return function(scope,element,attrs) {
      scope.$watch(
        function(scope) {
          return scope.$eval(attrs.compile);
        },function(value) {
          element.html(value);
          $compile(element.contents())(scope);
        }
     )};
}])

运行一下,完美的解决了问题。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读