Filters are built to perform template data processing, so their utilization outside the template will be infrequent. Nonetheless, AngularJS provides you with the ability to use filter functions via an injection of $filter
.
Suppose that you have an application, as follows:
(app.js) angular.module('myApp', []) .controller('Ctrl', function ($scope) { $scope.val = 1234.56789; });
In the view templates, the argument order is scrambled with the following format:
data | filter : optionalArgument
For this example, it would take the form in the template as follows:
<p>{{ val | number : 4 }}</p>
This will give the following result:
1,234.5679
In this example, it's cleanest to apply the filter in the view template, as the purpose of formatting the number is merely for readability. If, however, the number
filter is needed to be used in a controller, $filter
can be injected and used as follows:
(app.js) angular.module('myApp', [...