CHAPTER 4
Angular Filters are typically used to format expressions in bindings in your template. They transform the input data into a new, formatted data type.
You wish to format the amount of currency with a localized currency label.
Use the built-in currency filter and make sure you load the corresponding locale file for the user's language:
<html> |
Enter an amount and it will be displayed using Angular's default locale.
You can find the complete example on GitHub.
In our example, we explicitly load the German locale settings and, therefore, the default formatting will be in German. The English locale is shipped by default so there's no need to include the angular-locale_en.js file. If you remove the script tag, you will see the formatting change to English instead. This means, in order for a localized application to work correctly, you need to load the corresponding locale file. All available locale files can be seen on GitHub.
You wish to revert a user's text input.
Implement a custom filter, which reverts the input:
<body ng-app="MyApp"> |
You can find the complete example on GitHub.
Angular's filter function expects a filter name and a function as params. The function must return the actual filter function where you implement the business logic. In this example, it will only have an input param. The result will be returned after the for loop has reversed the input.
You wish to make your filter customizable by introducing config params.
Angular filters can be passed as a hash of params which can be directly accessed in the filter function:
<body ng-app="MyApp"> |
You can find the complete example on GitHub.
The suffix ! is passed as an option to the filter function and is appended to the output. Note that we check if an actual input exists since we don't want to render the suffix without any input.
You wish to filter a ul list of names.
In addition to strings as input, Angular's filters also work with arrays:
<body ng-app="MyApp"> |
We pass Peter as the single param to the exclude filter, which will render all names except Peter.
You can find the complete example on GitHub.
The filter implementation loops through all names and creates a result array excluding 'Peter'. Note that the actual syntax of the filter function didn't change at all from our previous example with the string input.
You wish to combine several filters to form a single result.
Filters can be chained using the Unix-like pipe syntax:
<body ng-app="MyApp"> |
The pipe symbol (|) is used to chain multiple filters together. First, we will start with the initial array of names. After applying the exclude filter, the array contains only ['Anton', 'John'] and afterwards we will sort the names in ascending order.
I leave the implementation of the sortAscending filter as an exercise to the reader.
You wish to unit test your new filter. Let us start with an easy filter, which renders a checkmark depending on a Boolean value:
<body ng-init="data = true"> |
Use the angular-seed project as a bootstrap again:
describe('MyApp Tabs', function() { |
The beforeEach loads the module and the it method injects the filter function for us. Note that it has to be called checkmarkFilter; otherwise, Angular can't inject our filter function correctly.