Very easy way of injecting a filter is to inject dependency with the name composed of the filter name and the Filter suffix.
1) Built-in Filter. AngularJS has built-in filter named filter that can be used to reduce array based on some condition. To use it, we have to inject it as filterFilter. Using this filter, you can specify condition by attributes or by text like example below. script.js. 2) Custom Filter. We also can use our own filter to be called inside controller. It is almost same like above by injecting the filter name yourFilterNameFilter but I want to show you the alternative way with built-in $filter. Let’s we create simple filter named hello. Introduction. In this article I will tell you how to use the $filter service in AngularJs. The $filter service can be used to filter the data, it is declared along with the $scope service. Here I will create a simple example on $filter that will help you understand how to use the $filter service. Step 1. First of all you need to add an external Angular.js file to your application, angular.min.js.  There are two ways (or methods) you can use a currency filter to format a string in an AngularJS app. In the first method, you can apply the filter to an HTML template (either using an expression or in a directive). The second method is to use the filter in a Controller using JavaScript.  We will see how to achieve that in this post. There are two ways to invoke filters from controllers in AngularJS. One approach is using the $filter service and the other is directly injecting the filters into the controller. Getting filters injected into the controller. Filters can be injected into the controller, but the name of the filter that is specified in the controller argument should be appended with the word “Filter”. As we have a reference to the filter available in the controller, we can invoke the filter directly from here. How to Use a Filter in a Controller in AngularJS. In this post, I am sharing how to use a filter in a controller in AngularJS. Very easy way of injecting a filter is to inject dependency with the name composed of the filter name and the Filter suffix: . E.g. the dependency customFilter injects custom filter. The injected component is a filter function. Note that you can use this approache to inject filter also to services and directives. Following example injects filter called custom. Another way is to inject $filter dependency to your controller and fetch required filter inside the controller. The filter functionality of AngularJS is often used inside the view when you need to filter a set of items in a collection. I had overlooked the usefulness this might have as a tool inside the controller as well. It can be very useful in “querying” information. Same with a search box that is filtering a local data source. This post just highlights the fact that you can use the filter in the controller as well if there is a situation where you want to find an object or objects in a collection using controller code. It is common to see filter used in view but apparently I found that in some conditions we need to use filter in controller. You have to know that in order to inject a filter, you need to use format yourFilterNameFilter. 2) Custom Filter. We also can use our own filter to be called inside controller. It is almost same like above by injecting the filter name yourFilterNameFilter but I want to show you the alternative way with built-in $filter. Let’s we create simple filter named hello. Using filters in controllers, services, and directives. You can also use filters in controllers, services, and directives. For this, inject a dependency with the name to your controller/service/directive. E.g. using the dependency numberFilter will inject the number filter. 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).
http://budiirawan.com/angular-js-use-filter-controller/ http://www.c-sharpcorner.com/UploadFile/cd7c2e/use-filter-service-in-angularjs/ http://www.encodedna.com/angularjs/tutorial/format-text-using-angularjs-currency-filter.htm https://dzone.com/articles/invoking-angular-js-filters http://blog.tompawlak.org/use-filter-in-controller-angularjs http://voryx.net/using-angularjs-filter-inside-the-controller/ http://budiirawan.com/angular-js-use-filter-controller/ https://docs.angularjs.org/guide/filter#!
how to use filter in angularjs controller