forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
bdb82b0
commit 310e3c4
Showing
18 changed files
with
138 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 0 additions & 60 deletions
60
docs/content/guide/dev_guide.templates.filters.creating_filters.ngdoc
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
44 changes: 0 additions & 44 deletions
44
docs/content/guide/dev_guide.templates.filters.using_filters.ngdoc
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
@ngdoc overview | ||
@name Filters | ||
@description | ||
|
||
A filter formats the value of an expression for display to the user. They can be used in view templates, | ||
controllers or services and it is easy to define your own filter. | ||
|
||
The underlying API is the {@link api/ng.$filterProvider filterProvider}. | ||
|
||
## Using filters in view templates | ||
|
||
Filters can be applied to expressions in view templates using the following syntax: | ||
|
||
{{ expression | filter }} | ||
|
||
E.g. the markup `{{ 12 | currency }}` formats the number 12 as a currency using the {@link api/ng.filter:currency `currency`} | ||
filter. The resulting value is `$12.00`. | ||
|
||
Filters can be applied to the result of another filter. This is called "chaining" and uses | ||
the following syntax: | ||
|
||
{{ expression | filter1 | filter2 | ... }} | ||
|
||
Filters may have arguments. The syntax for this is | ||
|
||
{{ expression | filter:argument1:argument2:... }} | ||
|
||
E.g. the markup `{{ 1234 | number:2 }}` formats the number 1234 with 2 decimal points using the | ||
{@link api/ng.filter:number `number`} filter. The resulting value is `1,234.00`. | ||
|
||
|
||
## Using filters in controllers and services | ||
|
||
You can also use filters in controllers and services. For this, add a dependency with the name `<filterName>Filter` | ||
to your controller or service. E.g. using the dependency `numberFilter` will inject the number filter. | ||
The injected argument is a function that takes the value to format as first argument and filter parameters | ||
starting with the second argument. | ||
|
||
The example below uses the filter called {@link api/ng.filter:filter `filter`}. | ||
This filter reduces arrays into sub arrays based on | ||
conditions. The filter can be applied in the view template with markup like | ||
`{{ctrl.array | filter:'a'}}`, which would do a fulltext search for "a". | ||
However, using a filter in a view template will reevaluate the filter on | ||
every digest, which can be costly if the array is big. | ||
|
||
The example below therefore calls the filter directly in the controller. | ||
By this, the controller is able to call the filter only when needed (e.g. when the data is loaded from the backend | ||
or the filter expression is changed). | ||
|
||
<doc:example module="FilterInControllerModule"> | ||
<doc:source> | ||
<script> | ||
angular.module('FilterInControllerModule', []). | ||
controller('FilterController', ['filterFilter', function(filterFilter) { | ||
this.array = [ | ||
{name: 'Tobias'}, | ||
{name: 'Jeff'}, | ||
{name: 'Brian'}, | ||
{name: 'Igor'}, | ||
{name: 'James'}, | ||
{name: 'Brad'} | ||
]; | ||
this.filteredArray = filterFilter(this.array, 'a'); | ||
}]); | ||
</script> | ||
|
||
<div ng-controller="FilterController as ctrl"> | ||
<div> | ||
All entries: | ||
<span ng-repeat="entry in ctrl.array">{{entry.name}} </span> | ||
</div> | ||
<div> | ||
Entries that contain an "a": | ||
<span ng-repeat="entry in ctrl.filteredArray">{{entry.name}} </span> | ||
</div> | ||
</div> | ||
</doc:source> | ||
</doc:example> | ||
|
||
|
||
## Creating custom filters | ||
|
||
Writing your own filter is very easy: just register a new filter factory function with | ||
your module. Internally, this uses the {@link api/ng.$filterProvider `filterProvider`}. | ||
This factory function should return a new filter function which takes the input value | ||
as the first argument. Any filter arguments are passed in as additional arguments to the filter | ||
function. | ||
|
||
The following sample filter reverses a text string. In addition, it conditionally makes the | ||
text upper-case. | ||
|
||
<doc:example module="MyReverseModule"> | ||
<doc:source> | ||
<script> | ||
angular.module('MyReverseModule', []). | ||
filter('reverse', function() { | ||
return function(input, uppercase) { | ||
var out = ""; | ||
for (var i = 0; i < input.length; i++) { | ||
out = input.charAt(i) + out; | ||
} | ||
// conditional based on optional argument | ||
if (uppercase) { | ||
out = out.toUpperCase(); | ||
} | ||
return out; | ||
} | ||
}); | ||
|
||
function Ctrl($scope) { | ||
$scope.greeting = 'hello'; | ||
} | ||
</script> | ||
|
||
<div ng-controller="Ctrl"> | ||
<input ng-model="greeting" type="greeting"><br> | ||
No filter: {{greeting}}<br> | ||
Reverse: {{greeting|reverse}}<br> | ||
Reverse + uppercase: {{greeting|reverse:true}}<br> | ||
</div> | ||
</doc:source> | ||
</doc:example> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.