Introduction To Filters In AngularJS

This article is about AngualrJS filters and their use. You can also create your own custom filters.

Filters

Filters format the values for display. Filters format the result which is required by the user to display. Filters can be used in services, templates and controllers. There are various built in filters in Angular. We can also create our own filters. API is used in $filterprovider. Filters are added to format the data.

Various Built in Filters are,

  • currency- Format a number to currency format.
  • date- Format a date to a specified format.
  • filter- It selects a subset of items in an array.
  • limitTo - It limits an array or a string into the specified number of elements or characters respectively.
  • lowercase- It formats a string to lowercase.
  • uppercase - It formats a string to upper case.
  • orderBy- It orders an array by an expression.
  • json - It formats an object to a JSON string.

Let’s understand with an example how we can use a filter to the expressions.

Filters can be added to an expression, using the pipe character “|”, followed by a filter.

e.g
  1. <div ng-app="myApp" ng-controller="myController">    
  2.     
  3.         <p>My Name: {{ firstName | uppercase }} {{ lastName | uppercase }}</p>    
  4.     
  5.     </div>    
  6.     
  7.     <script>    
  8.         angular.module('myApp', []).controller('myController'function ($scope) {    
  9.             $scope.firstName = "jasbeer",    
  10.             $scope.lastName = "singh"    
  11.         });    
  12.     </script>    

In this example, we have used uppercase filter. As you can see, we have provided firstName and lastName properties to $scope and in view, we have used filter as uppercase to both the properties, so the result will be in uppercase. Let’s run this and check the output.

 

Now, we can see that the firstName and lastName are in upper case letters because of the filters used.

We can also use filters with the Directives. Filters are added to the Directives by using the pipeline character “|”, followed by filter.

Let’s understand this with an example how we can use the filter with the Directives.

Example

View
  1. <head>    
  2.     <title></title>    
  3.     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>    
  4.     <script src="dirScript.js"></script>    
  5. </head>    
  6. <body>    
  7.     
  8.     <div ng-app="myApp" ng-controller="dirController">    
  9.     
  10.         <ul>    
  11.             <li ng-repeat="x in names | orderBy:'lname'">    
  12.                 {{ x.fname + ' ' + x.lname }}    
  13.             </li>    
  14.         </ul>    
  15.     
  16.     </div>    
  17. </body>   

Script

  1. angular.module('myApp', []).controller('dirController'function ($scope) {    
  2.     $scope.names = [    
  3.         { fname: 'Jasbeer', lname: 'Singh' },    
  4.         { fname: 'Jatinder', lname: 'Singh' },    
  5.         { fname: 'Randeep', lname: 'Singh' },    
  6.         { fname: 'Jasmeen', lname: 'Kaur' },    
  7.         { fname: 'Jasleen', lname: 'Kaur' },    
  8.     ];    
  9. });    
Here, in the example given above, we have used filter orderBy, which filters the names according to their lastname.

If we run this, we will get the names orderBy lastname and the output will be, as shown below.

 

Now, we will see an example, where the user uses filter.

View

  1. <head>    
  2.     <title></title>    
  3.     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>    
  4.     <script src="filterScript.js"></script>    
  5. </head>    
  6. <body>    
  7.     <div ng-app="myfilterApp" ng-controller="filterController">    
  8.     
  9.         <p>Type a letter in the input field to filter the names:</p>    
  10.     
  11.         <p><input type="text" ng-model="fname"></p>    
  12.     
  13.         <ul>    
  14.             <li ng-repeat="x in names | filter:fname">    
  15.                 {{ x }}    
  16.             </li>    
  17.         </ul>    
  18.     
  19.     </div>    
  20. </body>    
Script
  1. angular.module('myfilterApp', []).controller('filterController'function ($scope) {    
  2.     $scope.names = [    
  3.         'Jasbeer',    
  4.         'Jasmeen',    
  5.         'Randeep',    
  6.         'Sandeep',    
  7.         'Rashmi'    
  8.            
  9.     ];    
  10. });   
Now, if you run this, we will get all the names with a textbox to filter. If we input any letter, it will find the letter in names and will output the name having that letter.

In the output, we have entered and we have inputted letter R, so we got the names having letter R.

Custom Filters

We have seen built in filters, now we will create our own filters for use. We can do this by making our own filter and registering a new filter factory function with our module.

Let’s create a custom filter to make the words in lower case letters without using built in filter. It will format the upper case letters to lower case letters.

View

  1. <html xmlns="http://www.w3.org/1999/xhtml">    
  2. <head>    
  3.     <title></title>    
  4.     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>    
  5.     <script src="custom.js"></script>    
  6. </head>    
  7. <body>    
  8.     <ul ng-app="myApp" ng-controller="customController">    
  9.         <li ng-repeat="x in names">    
  10.             {{x | customFilter}}    
  11.         </li>    
  12.     </ul>    
  13. </body>    
  14. </html>    
Script
  1. var app = angular.module('myApp', []);    
  2. app.filter('customFilter'function () {    
  3.     return function (x) {    
  4.         var i, c, txt = "";    
  5.         for (i = 0; i < x.length; i++) {    
  6.             c = x[i];    
  7.                 c = c.toLowerCase();    
  8.             txt += c;    
  9.         }    
  10.         return txt;    
  11.     };    
  12. });    
  13. app.controller('customController'function ($scope) {    
  14.     $scope.names = ['JASBEER',    
  15.         'SINGH',    
  16.         'JATINDER',    
  17.         'SAAKIB',    
  18.         'WANI'];    
  19. });   

You can see here in the example given above that we have written all the names in capital letters and assigned to the property names of the $scope. In the view, we have applied our custom filter, which we have created in the script and converted all the capital letters to small and displayed on the view for the user. If we run this, we will get all the names in small letters.

Output

 

All the names are in small letters. In the script, we have created our custom filter.

  1. app.filter('customFilter'function () {    
  2.     return function (x) {    
  3.         var i, c, txt = "";    
  4.         for (i = 0; i < x.length; i++) {    
  5.             c = x[i];    
  6.                 c = c.toLowerCase();    
  7.             txt += c;    
  8.         }    
  9.         return txt;    
  10.     };    
  11. });     
It is used in the view to filter the names to capital letters.
  1. <li ng-repeat="x in names">    
  2.             {{x | customFilter}}    
  3.         </li>    
In the templates, the filters are executed when their input is changed.

This has covered the basics of filters in AngularJS. Please provide your valuable feedback after going through the article.