Filters In AngularJS

Introduction

AngularJS Filters cover many common uses such as formatting dates and currencies, limiting the number of items to be displayed, etc. The filters are very helpful to improve the workflow while building Angular apps.

One of the common use of filter is that it formats the value of expression for displaying  to the user. The filters can be used in any layer of AngularJS application i.e. views, controller or services. It can be also used in directives.

Filter in View templates

Filter can be used with expressions and it can be separated by pipe character.

Syntax:

    {{ expression | filter }}

Example:

The "currency" filter use to format a number as currency.

  1. <div>  
  2.     <div style="width:100%">  
  3.         <p><b>Currency format filter example </b></p>  
  4.         <p>Quantity :  
  5.             <inputtype="number" ng-model="quantity">  
  6.         </p>  
  7.         <p>Price :  
  8.             <inputtype="number" ng-model="price">  
  9.         </p>  
  10.         <p>Total = {{(quantity*price)|currency}}</p>  
  11.     </div>  
  12. </div>  
Output:

See result

Filter can be applied to the result of other filter i.e. we can create the chain of filters. So we applied multiple filters on expressions. We will take an example of applying multiple filters on expression later on this article.

Syntax:

 

    {{ expression | filter1 | filter2 | ... }}

We can also pass argument to filters. The syntax is as following.

Syntax:

    {{expression | filter:argument1:argument2:... }}

Example:

In the following first example code, filter formats the number 2311 with 2 decimal points using number filter. In the second example code, date is to be formatted to "dd/MM/yyyy" and in the third example code, date format to short month name.

demo.html

  1. <div style="width:100%">  
  2.     <p><b>Filter with argument example </b></p>  
  3.     <p>Number format example: {{2311|number:2}}</p>  
  4.     <p>Date format example: {{1505556671920|date:'dd-MM-yyyy'}}</p>  
  5.     <p>Date format example: {{1505556671920|date:"'Month:'MMM"}}</p>  
  6. </div>  
Output:

run

Filters in controllers, services, and directives

We can also use the filters in controllers, services and directives. To do this, we need to inject a dependency with "<filterName>Filter". For example, if we want to use the number filter in controllers, services and directives then we need to inject dependency "numberFilter".

In the following example, we used the filter called "filter" and this filters the records from the array based on the condition.

DemoController.js
  1. app.controller("demoController", ['$scope''filterFilter', function($scope, filterFilter) {  
  2.     $scope.employeeArray = [{  
  3.         name: 'Tejas'  
  4.     }, {  
  5.         name: 'Jignesh'  
  6.     }, {  
  7.         name: 'Rakesh'  
  8.     }, {  
  9.         name: 'Nirav'  
  10.     }, {  
  11.         name: 'Hiten'  
  12.     }, {  
  13.         name: 'Varun'  
  14.     }, {  
  15.         name: 'Chirag'  
  16.     }];  
  17.     $scope.filteredArray = filterFilter($scope.employeeArray, 's');  
  18. }]);  
Demo.html
  1. <div ng-controller="demoController" style="width:100%">  
  2.     <p><b>Filter in controller example </b></p>  
  3.     <div>  
  4.         <p>All Employee List:</p>  
  5.         <ul>  
  6.             <ling-repeat="entry in employeeArray">{{entry.name}}</li>  
  7.         </ul>  
  8.     </div>  
  9.     <div>  
  10.         <p>Employee name List that contain an "s":</p>  
  11.         <ul>  
  12.             <ling-repeat="entry in filteredArray">{{entry.name}}</li>  
  13.         </ul>  
  14.     </div>  
  15. </div>  
Output:

result

Apply multiple filters

As we know, we can apply multiple filters in an expression. All filters are separated by pipe character (|). Filter is applied on the result of previous filter if any and that will create the chain of filters.

In the following example, I have applied two filters, first filter is employee that contains character “s” and other filter sort the result of first filter. In this example I have used controller same as in the previous example.

Demo.html:
  1. <div ng-controller="demoController" style="width:100%">  
  2.     <p><b>Apply multiple filters</b></p>  
  3.     <div>  
  4.         <p>Employee List:</p>  
  5.         <ul>  
  6.             <ling-repeat="entry in employeeArray | filter:'s' | orderBy:'name'">{{entry.name}}</li>  
  7.         </ul>  
  8.     </div>  
  9. </div>  
Output:

Output

Summary

Filters are very powerful feature in AngularJS for transforming our data, so it is easy to scale and reuse.

Hope this will help you.

 


Similar Articles