Using filters in a view template
A filter is also a service that handles the conversion of the input content to better display to the user.
The filter can be used in the {{}} tag in the template:
- {{ expression | filter:arg1:arg2}}
The Angularjs ng module implements some pre-set filters, such as currency, number, etc., which can be used directly. For example, the following example uses the currency filter for the number 12, and the result will be "$12.00":
- {{| Currency}}
Filters can also have parameters, for example, the following example formats a number as "1,234.00":
- {{1234| Number:2}}
Filters can be applied to the output of another filter, like pipelining, with the following syntax:
- {{expression| Filter1| Filter2| ...}}
Example of using a filter (the first page of "Using Filter transformation Output" in http://www.hubwiz.com/course/54f3ba65e564e50cfccbad4b/).
Using filters in your code
Don't forget that the filter is also a service, so you can inject it into your code.
Unlike the normal service, when the filter is registered in the injector, the name is appended with a suffix: filter. For example, the service name of the number filter is: Numberfilter, and the service name of the currency filter is: Currencyfilter.
Usually our code is encapsulated in three places: controllers, services, instructions. These places support direct injection of services, such as:
- Angular. Module(' MyModule ', [])
- . Controller(function($scope,numberfilter) {
- //...
- })
Sometimes you need to explicitly call the filter through the injector, then use the Invoke () method of the injector:
- Angular. Injector([' ng '])
- . Invoke(function(numberfilter) {
- //...
- })
In short, remember that a filter is a service, except that the name needs to append the filter suffix, and the invocation method of other services is no different.
Example (the "Use Filter conversion output" on the second page in http://www.hubwiz.com/course/54f3ba65e564e50cfccbad4b/) injects the number and currency filters into the controller, The format of total is implemented.
Creating filters
Like directives, filters are also a special service, compared to creating a common service:
- The service must be registered using the filter () interface of the module
- Object Factory/factory method must be provided
- Object engineering must return a filter function whose first argument is an input variable
- Define Filter class factory
- var filterfactory = function() {
- //define Filter function
- var filter = function(input,Extra_args) {
- //process input and generate output
- return output
- }
- };
- Registering filters on the module
- Angular. Module("Somemodule", [])
- . Filter("FilterName",filterfactory);
Adding parameters to the filter
The behavior of the filter can be adjusted by additional parameters. For example, we want to refine the example in the previous section so that it can support only the first letter of each word in uppercase.
This can be accomplished by passing in additional parameters in the filter function returned by the filter class factory.
- var filter = function(input,argument1,argument2) {...}
When using filters, additional parameters are prefixed by: introduction, e.g.
- {{expression| Filter:argument1:argument2}}
Reference: http://www.hubwiz.com
Angularjs using filter conversion output (ANGULARJS series last article)