Skip to content

Commit 3662140

Browse files
committed
docs(guide/filter): Refactor filter guide docs
This refactors the filter guide docs into a single file. Also removes out of date references to the fact that Angular used to enhance Arrays while evaluating expressions.
1 parent ed8640b commit 3662140

18 files changed

+138
-193
lines changed

docs/content/api/index.ngdoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The documentation is organized into **modules** which contain various components
99
These components are directives, services, filters, providers, types, global APIs and testing mocks.
1010

1111
<div class="alert alert-info">
12-
**Angular Namspaces `$` and `$$`**
12+
**Angular Namespaces `$` and `$$`**
1313

1414
To prevent accidental name collisions with your code,
1515
Angular prefixes names of public objects with `$` and names of private objects with `$$`.

docs/content/guide/concepts.ngdoc

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ in the rest of the documentation.
8686
Applied to the example above, the markup directs Angular to "take the data we got from the input widgets
8787
and multiply them together".
8888

89-
The example above also contains a <a name="filter">"{@link dev_guide.templates.filters filter}"</a>.
89+
The example above also contains a <a name="filter">"{@link filter filter}"</a>.
9090
A filter formats the value of an expression for display to the user.
9191
In the example above, the filter {@link api/ng.filter:currency `currency`} formats a number
9292
into an output that looks like money.
@@ -144,7 +144,7 @@ allow to enter and calculate the costs in different currencies and also pay the
144144
<span ng-repeat="c in invoice.currencies">
145145
{{invoice.total(c) | currency:c}}
146146
</span>
147-
<button ng-click="invoice.pay()">Pay</button>
147+
<button class="btn" ng-click="invoice.pay()">Pay</button>
148148
</div>
149149
</div>
150150
</file>
@@ -244,7 +244,7 @@ Let's refactor our example and move the currency conversion into a service in an
244244
<span ng-repeat="c in invoice.currencies">
245245
{{invoice.total(c) | currency:c}}
246246
</span>
247-
<button ng-click="invoice.pay()">Pay</button>
247+
<button class="btn" ng-click="invoice.pay()">Pay</button>
248248
</div>
249249
</div>
250250
</file>
@@ -370,7 +370,7 @@ The following example shows how this is done with Angular:
370370
<span ng-repeat="c in invoice.currencies">
371371
{{invoice.total(c) | currency:c}}
372372
</span>
373-
<button ng-click="invoice.pay()">Pay</button>
373+
<button class="btn" ng-click="invoice.pay()">Pay</button>
374374
</div>
375375
</div>
376376
</file>

docs/content/guide/controller.ngdoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ logic. Angular offers {@link databinding databinding} for automatic DOM manipula
109109
you have to perform your own manual DOM manipulation, encapsulate the presentation logic in
110110
{@link guide/directive directives}.
111111
- Input formatting — Use {@link forms angular form controls} instead.
112-
- Output filtering — Use {@link dev_guide.templates.filters angular filters} instead.
112+
- Output filtering — Use {@link filter angular filters} instead.
113113
- Sharing stateless or stateful code across Controllers — Use {@link dev_guide.services angular
114114
services} instead.
115115
- Managing the life-cycle of other components (for example, to create service instances).

docs/content/guide/dev_guide.templates.filters.creating_filters.ngdoc

-60
This file was deleted.

docs/content/guide/dev_guide.templates.filters.ngdoc

-23
This file was deleted.

docs/content/guide/dev_guide.templates.filters.using_filters.ngdoc

-44
This file was deleted.

docs/content/guide/dev_guide.unit-testing.ngdoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ Notice that the test is not only much shorter but it is easier to follow what is
259259
that such a test tells a story, rather then asserting random bits which don't seem to be related.
260260

261261
## Filters
262-
{@link api/ng.$filter Filters} are functions which transform the data into user readable
262+
{@link api/ng.$filterProvider Filters} are functions which transform the data into user readable
263263
format. They are important because they remove the formatting responsibility from the application
264264
logic, further simplifying the application logic.
265265

docs/content/guide/expression.ngdoc

+1-39
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
Expressions are JavaScript-like code snippets that are usually placed in bindings such as `{{
66
expression }}`. Expressions are processed by the {@link api/ng.$parse $parse}
7-
service.
7+
service. Expressions are often post processed using {@link filter filters} to create a more user-friendly format.
88

99
For example, these are all valid expressions in angular:
1010

1111
* `1+2`
12-
* `3*10 | currency`
1312
* `user.name`
1413

1514

@@ -29,9 +28,6 @@ You can think of Angular expressions as JavaScript expressions with following di
2928
* **No Control Flow Statements:** you cannot do any of the following in angular expression:
3029
conditionals, loops, or throw.
3130

32-
* **Filters:** you can pass result of expression evaluations through filter chains. For example
33-
to convert date object into a local specific human-readable format.
34-
3531
If, on the other hand, you do want to run arbitrary JavaScript code, you should make it a
3632
controller method and call the method. If you want to `eval()` an angular expression from
3733
JavaScript, use the {@link api/ng.$rootScope.Scope#methods_$eval `$eval()`} method.
@@ -150,37 +146,3 @@ You cannot write a control flow statement in an expression. The reason behind th
150146
Angular philosophy that application logic should be in controllers, not in the view. If you need a
151147
conditional, loop, or to throw from a view expression, delegate to a JavaScript method instead.
152148

153-
154-
## Filters
155-
156-
When presenting data to the user, you might need to convert the data from its raw format to a
157-
user-friendly format. For example, you might have a data object that needs to be formatted
158-
according to the locale before displaying it to the user. You can pass expressions through a chain
159-
of filters like this:
160-
161-
name | uppercase
162-
163-
The expression evaluator simply passes the value of name to {@link
164-
api/ng.filter:uppercase `uppercase`} filter.
165-
166-
Chain filters using this syntax:
167-
168-
value | filter1 | filter2
169-
170-
You can also pass colon-delimited arguments to filters, for example, to display the number 123
171-
with 2 decimal points:
172-
173-
123 | number:2
174-
175-
# The $
176-
177-
You might be wondering, what is the significance of the $ prefix? It is simply a prefix that
178-
angular uses, to differentiate its API names from others. If angular didn't use $, then evaluating
179-
`a.length()` would return undefined because neither a nor angular define such a property.
180-
181-
Consider that in a future version of Angular we might choose to add a length method, in which case
182-
the behavior of the expression would change. Worse yet, you, the developer, could create a length
183-
property and then we would have a collision. This problem exists because Angular augments existing
184-
objects with additional behavior. By prefixing its additions with $ we are reserving our namespace
185-
so that angular developers and developers who use Angular can develop in harmony without collisions.
186-

docs/content/guide/filter.ngdoc

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
@ngdoc overview
2+
@name Filters
3+
@description
4+
5+
A filter formats the value of an expression for display to the user. They can be used in view templates,
6+
controllers or services and it is easy to define your own filter.
7+
8+
The underlying API is the {@link api/ng.$filterProvider filterProvider}.
9+
10+
## Using filters in view templates
11+
12+
Filters can be applied to expressions in view templates using the following syntax:
13+
14+
{{ expression | filter }}
15+
16+
E.g. the markup `{{ 12 | currency }}` formats the number 12 as a currency using the {@link api/ng.filter:currency `currency`}
17+
filter. The resulting value is `$12.00`.
18+
19+
Filters can be applied to the result of another filter. This is called "chaining" and uses
20+
the following syntax:
21+
22+
{{ expression | filter1 | filter2 | ... }}
23+
24+
Filters may have arguments. The syntax for this is
25+
26+
{{ expression | filter:argument1:argument2:... }}
27+
28+
E.g. the markup `{{ 1234 | number:2 }}` formats the number 1234 with 2 decimal points using the
29+
{@link api/ng.filter:number `number`} filter. The resulting value is `1,234.00`.
30+
31+
32+
## Using filters in controllers and services
33+
34+
You can also use filters in controllers and services. For this, add a dependency with the name `<filterName>Filter`
35+
to your controller or service. E.g. using the dependency `numberFilter` will inject the number filter.
36+
The injected argument is a function that takes the value to format as first argument and filter parameters
37+
starting with the second argument.
38+
39+
The example below uses the filter called {@link api/ng.filter:filter `filter`}.
40+
This filter reduces arrays into sub arrays based on
41+
conditions. The filter can be applied in the view template with markup like
42+
`{{ctrl.array | filter:'a'}}`, which would do a fulltext search for "a".
43+
However, using a filter in a view template will reevaluate the filter on
44+
every digest, which can be costly if the array is big.
45+
46+
The example below therefore calls the filter directly in the controller.
47+
By this, the controller is able to call the filter only when needed (e.g. when the data is loaded from the backend
48+
or the filter expression is changed).
49+
50+
<doc:example module="FilterInControllerModule">
51+
<doc:source>
52+
<script>
53+
angular.module('FilterInControllerModule', []).
54+
controller('FilterController', ['filterFilter', function(filterFilter) {
55+
this.array = [
56+
{name: 'Tobias'},
57+
{name: 'Jeff'},
58+
{name: 'Brian'},
59+
{name: 'Igor'},
60+
{name: 'James'},
61+
{name: 'Brad'}
62+
];
63+
this.filteredArray = filterFilter(this.array, 'a');
64+
}]);
65+
</script>
66+
67+
<div ng-controller="FilterController as ctrl">
68+
<div>
69+
All entries:
70+
<span ng-repeat="entry in ctrl.array">{{entry.name}} </span>
71+
</div>
72+
<div>
73+
Entries that contain an "a":
74+
<span ng-repeat="entry in ctrl.filteredArray">{{entry.name}} </span>
75+
</div>
76+
</div>
77+
</doc:source>
78+
</doc:example>
79+
80+
81+
## Creating custom filters
82+
83+
Writing your own filter is very easy: just register a new filter factory function with
84+
your module. Internally, this uses the {@link api/ng.$filterProvider `filterProvider`}.
85+
This factory function should return a new filter function which takes the input value
86+
as the first argument. Any filter arguments are passed in as additional arguments to the filter
87+
function.
88+
89+
The following sample filter reverses a text string. In addition, it conditionally makes the
90+
text upper-case.
91+
92+
<doc:example module="MyReverseModule">
93+
<doc:source>
94+
<script>
95+
angular.module('MyReverseModule', []).
96+
filter('reverse', function() {
97+
return function(input, uppercase) {
98+
var out = "";
99+
for (var i = 0; i < input.length; i++) {
100+
out = input.charAt(i) + out;
101+
}
102+
// conditional based on optional argument
103+
if (uppercase) {
104+
out = out.toUpperCase();
105+
}
106+
return out;
107+
}
108+
});
109+
110+
function Ctrl($scope) {
111+
$scope.greeting = 'hello';
112+
}
113+
</script>
114+
115+
<div ng-controller="Ctrl">
116+
<input ng-model="greeting" type="greeting"><br>
117+
No filter: {{greeting}}<br>
118+
Reverse: {{greeting|reverse}}<br>
119+
Reverse + uppercase: {{greeting|reverse:true}}<br>
120+
</div>
121+
</doc:source>
122+
</doc:example>
123+

docs/content/guide/index.ngdoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ In Angular applications, you move the job of filling page templates with data fr
3636

3737
* {@link api/ngRoute.$route Views and routes (see the example)}
3838

39-
* {@link guide/dev_guide.templates.filters Filters}
39+
* {@link guide/filter Filters}
4040

4141
* {@link guide/forms Forms} and [Concepts of AngularJS Forms](http://mrbool.com/the-concepts-of-angularjs-forms/29117)
4242

0 commit comments

Comments
 (0)