Can a filter be an unclean function?

The following works:

<script>
angular.module('myApp', [])
.filter('myFilter', ['$rootScope', function($rootScope) {
  return function(v) {
     return $rootScope.capitalize ? (v && v.toUpperCase()) : v;
  };
}])
.controller('myController', ['$rootScope', '$scope', function($rootScope, $scope) {
  $scope.flipCapitalize = function() {
    $rootScope.capitalize = !$rootScope.capitalize;
  }
}]);
</script>
{{"Hello" | myFilter }}

<div ng-controller="myController">
  <button ng-click="flipCapitalize()">flip</button>
</div>

The word “Hello” on the screen flips between the mixed case and upper case when you press the button.

But Angular does not “know”, it must repeatedly call the filter function. He just does it because a click restarts the digest view, and he does everything again.

My question is: is behavior guaranteed? Can I always assume that the filter will be re-looped in the digest cycle, and can I use any data in any area that I can find? Or am I just lucky?

: , ? , , , ? ?

+4
1

angular docs, , , "stateful", $stateful .

, , Angular, . , .

, $stateful, , $digest.

, stateful, :

.filter('myFilter', ['$rootScope', function($rootScope) {
  var filter = function(v) {
     return $rootScope.capitalize ? (v && v.toUpperCase()) : v;
  };
  filter.$stateful = true;
  return filter;
}])
+1

All Articles