Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 7c60264

Browse files
committedSep 16, 2014
docs(guide/filters): add a note about $stateful flag and stateful filters
1 parent 76741a9 commit 7c60264

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed
 

‎docs/content/guide/filter.ngdoc

+49-4
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,16 @@ This factory function should return a new filter function which takes the input
8787
as the first argument. Any filter arguments are passed in as additional arguments to the filter
8888
function.
8989

90+
The filter function should be a [pure function](http://en.wikipedia.org/wiki/Pure_function), which
91+
means that it should be stateless and idempotent. Angular relies on these properties and executes
92+
the filter only when the inputs to the function change.
93+
9094
The following sample filter reverses a text string. In addition, it conditionally makes the
9195
text upper-case.
9296

93-
<example module="myReverseModule">
97+
<example module="myReverseFilterApp">
9498
<file name="index.html">
95-
<div ng-controller="Controller">
99+
<div ng-controller="MyController">
96100
<input ng-model="greeting" type="text"><br>
97101
No filter: {{greeting}}<br>
98102
Reverse: {{greeting|reverse}}<br>
@@ -101,7 +105,7 @@ text upper-case.
101105
</file>
102106

103107
<file name="script.js">
104-
angular.module('myReverseModule', [])
108+
angular.module('myReverseFilterApp', [])
105109
.filter('reverse', function() {
106110
return function(input, uppercase) {
107111
input = input || '';
@@ -116,12 +120,53 @@ text upper-case.
116120
return out;
117121
};
118122
})
119-
.controller('Controller', ['$scope', function($scope) {
123+
.controller('MyController', ['$scope', function($scope) {
120124
$scope.greeting = 'hello';
121125
}]);
122126
</file>
123127
</example>
124128

129+
130+
## Stateful filters
131+
132+
It is strongly discouraged to write filters that are stateful, because the execution of those can't
133+
be optimized by Angular, which often leads to performance issues. Many stateful filters can be
134+
converted into stateless filters just by exposing the hidden state as a model and turning it into an
135+
argument for the filter.
136+
137+
If you however do need to write a stateful filter, you have to mark the filter as `$stateful`, which
138+
means that it will be executed one or more times during the each `$digest` cycle.
139+
140+
<example module="myStatefulFilterApp">
141+
<file name="index.html">
142+
<div ng-controller="MyController">
143+
Input: <input ng-model="greeting" type="text"><br>
144+
Decoration: <input ng-model="decoration.symbol" type="text"><br>
145+
No filter: {{greeting}}<br>
146+
Reverse: {{greeting | decorate}}<br>
147+
</div>
148+
</file>
149+
150+
<file name="script.js">
151+
angular.module('myStatefulFilterApp', [])
152+
.filter('decorate', ['decoration', function(decoration) {
153+
154+
function decorateFilter(input) {
155+
return decoration.symbol + input + decoration.symbol;
156+
}
157+
decorateFilter.$stateful = true;
158+
159+
return decorateFilter;
160+
}])
161+
.controller('MyController', ['$scope', 'decoration', function($scope, decoration) {
162+
$scope.greeting = 'hello';
163+
$scope.decoration = decoration;
164+
}])
165+
.value('decoration', {symbol: '*'});
166+
</file>
167+
</example>
168+
169+
125170
## Testing custom filters
126171

127172
See the [phonecat tutorial](http://docs.angularjs.org/tutorial/step_09#test) for an example.

0 commit comments

Comments
 (0)