在Angular JS中使用带有ng-bind-html的自定义过滤器会导致过滤器函数被调用两次

时间:2015-08-26 13:57:59

标签: angularjs

HTML:

<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.angularjs.org/1.4.3/angular.js"></script>
    <script src="script.js"></script>
  </head>
  <body ng-app="app" ng-controller="controller">
      <div ng-bind="welcomeText | wrap"></div> <!-- ng-bind hits filter once -->
      <div ng-bind-html="welcomeText | wrap"></div> <!-- ng-bind-html hits filter twice -->
      <div>Hit count: {{hitCount.count}}</div>
  </body>
</html>

JavaScript的:

var hitCount = { count: 0 };

angular
    .module("app", [])
    .controller("controller", [ "$scope", function ($scope) {
        $scope.hitCount = hitCount;
        $scope.welcomeText = "Hello";
    }])
    .filter("wrap", function($sce) {
        return function(input) {
            hitCount.count++;
            return $sce.trustAsHtml("[" + input + "]");
        };
    });

ng-bind会导致过滤器函数被调用一次,正如我所料。

但是,ng-bind-html会导致过滤器函数被调用两次。

这是否有原因,或者这是一个错误?

Plunker:http://plnkr.co/edit/ZyVB7yJk0BIxmFNUy8uq?p=preview

0 个答案:

没有答案