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
会导致过滤器函数被调用两次。
这是否有原因,或者这是一个错误?