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中綁定的事件是不起作用的,即不會被編譯的。
如果,你想在這裏面綁定事件是行不通的。那麼該如何解決這個問題,我在我的另一篇博客中會講到。

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