angularjs中ngbind和ngbindhtml的区别

ngbind是用於单向绑定的,即用於单方面展示模型中的值。通常是一个字符串。
ngbindhtml也是用来展示模型中的字符串。这是个特殊的字符串,这个字符串中包含html标签。在这里面html标签会被解释,而在ngbind中元素标记会被当做普通的字符串。

<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
    <meta charset="UTF-8">
    <title>bind</title>
    <script type="text/javascript" src="../js/angular.min.js"></script>
    <script type="text/javascript" src="../js/angular-sanitize.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('app',['ngSanitize']);
        app.controller('MyCtrl', function($scope){
            $scope.str = '<span>aaa<strong>bbb</strong></span>';
        })
    </script>
</head>
<body ng-controller="MyCtrl">
    <div ng-bind="str"></div>
    <div ng-bind-html="str"></div>
</body>
</html>

同时,有一点需要注意的是,angularjs处于安全性的考虑,在ngbindhtml中绑定的事件是不起作用的,即不会被编译的。
如果,你想在这里面绑定事件是行不通的。那么该如何解决这个问题,我在我的另一篇博客中会讲到。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章