AngularJS动态绑定html

在web开发中我们经常需要将动态拼接的html字符串绑定到页面DOM上。

AngularJS中我们可以使用指令ng-bind-html来绑定动态Html,它会将计算出来的表达式结果用innerHtml绑定到html。

但是AngularJS默认是不相信添加的html内容的,所以我们需要调用$sce(Strict Contextual Escaping)服务来解决问题。

  • $sce is included by default starting with angular 1.2

<!DOCTYPE html>
<html ng-app ="myApp">
<head>
<meta charset="utf-8">
<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
<style>
    
body{
  border: 5px solid #FF851B;
  padding: 10px;
}
.info{
  color:#0074D9;
}
.age{
  color:#FF4136;
}

</style>
</head>
<body ng-controller="myCtrl">
    <div ng-bind-html="html"></div>
    <script>
    angular.module('myApp', [])
    .controller("myCtrl", ["$scope","$sce",function($scope,$sce) {
         $scope.myage = 16;
 
         $scope.myInfo = {
                name:"chenjy"
         };
         var html ="<div>my name:<span class='info'>chenjy</span>,my age:<span class='age'>16</span></div>";
         
         $scope.html = $sce.trustAsHtml(html);
        
    }]);
    </script>
</body>
</html>

1487780-20181015165222855-897223494.png

对于静态html这就够了,但是我们如果需要用到AngularJS强大的双向数据绑定能力


var html ="<div>my name:<span class='info'>{{myInfo.name}}</span>,my age:<span class='age'>{{myage}}</span></div>";

1487780-20181015165234080-1954464677.png

ng-bind-html并不会和$scope双向绑定,并且ng-click等指令也不会得到compile

  • 我们可以借助$compile编译html

<body ng-controller="myCtrl">
    <script>
    angular.module('myApp', [])
    .controller("myCtrl", ["$scope","$compile",function($scope,$compile) {
         $scope.myage = 16;
 
         $scope.myInfo = {
                name:"chenjy"
         };
         
         var template ="<div>my name:<span class='info'>{{myInfo.name}}</span>,my age:<span class='age'>{{myage}}</span></div>";
         var ele =  $compile(template)($scope);
         angular.element(document.body).append(ele);
         
    }]);
    </script>
</body>

但是AngularJS中我们最好直接写在directivelink中,此时编译阶段已经结束我们可以手动编译html。


.directive("myDir", ["$compile",function($compile) {
                    return {
                        restrict: "E",
                        link: function(scope, iElement, iAttrs) {
                            var template = "<div>my name:<span class='info'>{{myInfo.name}}</span>,my age:<span class='age'>{{myage}}</span></div><input type='text' ng-model='myage' />";
                            var ele = $compile(template)(scope);
                            iElement.append(ele);
                        }
                    };
                }]);

下面贴一个网上看到的比较通用的compile例子


<!DOCTYPE html>
<html ng-app ="myApp">
<head>
<meta charset="utf-8">
<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
<style>
    
body{
  border: 5px solid #FF851B;
  padding: 10px;
}
.info{
  color:#0074D9;
}
.age{
  color:#FF4136;
}

</style>
</head>
<body ng-controller="myCtrl">
    <html-compile html='{{html}}'></html-compile>
    <script>
    angular.module('myApp', [])
    .controller("myCtrl", ["$scope","$sce",function($scope,$sce) {
         $scope.myage = 16;
 
         $scope.myInfo = {
                name:"chenjy"
         };
         
         $scope.changeAge = function(){
            $scope.myage ++;
         };
         
         $scope.html ="<button ng-click='changeAge()'>change</button><div>my name:<span class='info'>{{myInfo.name}}</span>,my age:<span class='age'>{{myage}}</span></div>";
         
    }]).directive("htmlCompile", ["$compile", function($compile) {
        return {
            replace: true,
            restrict: 'EA',
            link: function(scope, elm, iAttrs) {
                var DUMMY_SCOPE = {
                        $destroy: angular.noop
                    },
                    root = elm,
                    childScope,
                    destroyChildScope = function() {
                        (childScope || DUMMY_SCOPE).$destroy();
                    };
                
                // 监听html值
                iAttrs.$observe("html", function(html) {
                    /**
                      * 当传入html的时候 先尝试销毁子scope,然后创建一个子scope,compile当前html,替换掉当前DOM
                      **/                       
                    if (html) {
                        destroyChildScope();
                        childScope = scope.$new(false);
                        var content = $compile(html)(childScope);
                        root.replaceWith(content);
                        root = content;
                    }
                    // 在父scope销毁的时候,销毁该子scope
                    scope.$on("$destroy", destroyChildScope);
                });
            }
        };
    }]);
    </script>
</body>
</html>

转载于:https://www.cnblogs.com/chenjy1225/p/9791937.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值