Understanding Filters In AngularJS - Part 6

This article explains filters with a sample application and its usage. In my previous article, we saw the understanding of Directives with a sample application. Here are the series of article written on AngularJS,

What is Filters

We can use filter format data for displaying it to the user. The formatted value of an expression is applied to your output data in AugularJs. We can use built-in Filters and create own filters like custom filters.

Filters Type

We can implement the following types of directives in AngularJs.

1. Currency

2. Date

3. Filter

4. LimitTo

5. JSON

6. Lowercase

7. Uppercase

8. OrderBy

It is enabled to access the AngularJS built in filters like currency, date, limitTo, number and so on. We will discuss list of the filters details as in the following table.

List of AngularJS Filters Table

S.NoFilter NameFilter Description
1CurrencyIt is used to formats number as currency
2DateIt is used to date formats
3FilterIt is used find the item from array
4LowercaseIt is used to convert string as lowercase
5LimitToIt is containing new array with specified number of items
6JsonIt is used to convert javascript object to json object
7OrderByIt is used to order by array
8UppercaseIt is used to convert string as uppercase

How to use filters

  1. {{ expression | filter_name }}  
  2. {{ expression | filter_name1 | filter_name2 | ... }}  

Currency

  1. {{ currency_expression | currency : symbol : fractionSize}}  

Lowercase

  1. {{ lowercase_expression | lowercase}}  

Uppercase

  1. {{ uppercase_expression | uppercase}}  

OrderBy

  1. {{ orderBy_expression | orderBy : expression : reverse}}  

Number

  1. {{ number_expression | number : fractionSize}}  

LimitTo

  1. {{ limitTo_expression | limitTo : limit : begin}}  

JSON

  1. {{ json_expression | json : spacing}}  

Filter

  1. {{ filter_expression | filter : expression : comparator}}  

Date

  1. {{ date_expression | date : format : timezone}}  

Step 1:

Open Visual Studio 2015 and go to file menu and point new and then click new project. New ASP.NET project window will open, you can select a ASP.NET Web Application and type Project Name AngularJsFiltersDemo, Choose the project location path and then click OK button.

New ASP.NET project window will open, you can select a Empty Template with No Authentication and then click OK button.

 
Go to Solution Explorer and right click the project name and then click Manage NuGet Packages.
 
  

NuGet Package Manager window will open and you can type AngularJS and browse. Also select AngularJS.Core and click Install button.

 

Preview window will open and you can see the AngularJS version installing details. Click OK button and after it is successfully installed in AngularJS, you can see the following,

 
You can see AngularJsFiltersDemo project structure as in the following screenshot.
 
 
 
Add CurrencyView.html

Go to Solution Explorer and right click the project name and Add, then click the HTML Page. You can type the item name CurrencyView.html and click OK button.

Add CurrencyController.js

Go to Solution Explorer and right click the project name and Add, then click the JavaScript file. You can type the item name CurrencyController.js and click OK button.

Step 2: CurrencyView.html code here,

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title> San2debug.net | AngularJs Filters Application Demo </title>  
  5.     <meta charset="utf-8" />  
  6.     <script src="Scripts/angular.min.js"></script>  
  7.     <script src="CurrencyController.js"></script>   
  8. </head>  
  9. <body>  
  10.     <h2> AngularJs Salary Calculation Application Demo </h2>  
  11.     <div ng-app="mainApp" ng-controller="CurrencyController">  
  12.         Basic Salary: <input type="number" ng-model="basic" /><br /><br />  
  13.         HRA:  <input type="number" ng-model="hra" /><br /><br />  
  14.         Food Allowance:  <input type="number" ng-model="food" /><br /><br />  
  15.         <hr />  
  16.         <strong>Total Salary</strong>={{ basic + hra + food | currency}}  
  17.       </div>  
  18. </body>  
  19. </html>  

CurrencyController.js code here,

  1. var mainApp = angular.module('mainApp', []);  
  2. mainApp.controller('CurrencyController'function ($scope) {  
  3.     $scope.basic = 7000;  
  4.     $scope.hra = 2300;  
  5.     $scope.food = 1200;  
  6. });  

Add EmployeeView.html

Go to Solution Explorer and right click the project name and Add, then click the HTML Page. You can type the item name EmployeeView.html and click OK button.

Add EmployeeController.js

Go to Solution Explorer and right click the project name and Add, then click the JavaScript file. You can type the item name EmployeeController.js and click OK button.

EmployeeView.html code here

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title> San2debug.net | AngularJs Filters Application Demo </title>  
  5.     <meta charset="utf-8" />  
  6.     <script src="Scripts/angular.min.js"></script>  
  7.     <script src="EmployeeController.js"></script>   
  8. </head>  
  9. <body>  
  10.     <center>  
  11.         <h2> AngularJs Filters Application Demo </h2>  
  12.         <div ng-app="mainApp" ng-controller="EmployeeController">  
  13.             <table border="1" cellpadding="3" cellspacing="6" width="50%">  
  14.                 <tr>  
  15.                     <td>First Name:</td>  
  16.                     <td><input type="text" ng-model="employee.firstName"></td>  
  17.                 </tr>  
  18.                 <tr>  
  19.                     <td>Last Name:</td>  
  20.                     <td><input type="text" ng-model="employee.lastName"></td>  
  21.                 </tr>  
  22.                 <tr>  
  23.                     <td>Salary:</td>  
  24.                     <td><input type="number" ng-model="employee.salary"></td>  
  25.                 </tr>  
  26.             </table>  
  27.             <hr />  
  28.             <table border="0">  
  29.                 <tr>  
  30.                     <td><strong>Full Name (in Capital Letters): </strong>{{employee.fullName() | uppercase}} <br /> </td>  
  31.                 </tr>  
  32.                 <tr>  
  33.                     <td><strong>Full Name (in Normal Letters): </strong>{{employee.fullName() | lowercase}} <br /> </td>  
  34.                 </tr>  
  35.                 <tr>  
  36.                     <td><strong>Salary: </strong>{{employee.salary | currency}}  <br /></td>  
  37.                 </tr>  
  38.             </table>  
  39.         </div>  
  40.     </center>  
  41. </body>  
  42. </html>  

EmployeeController.js code here,

  1. var mainApp = angular.module('mainApp', []);  
  2. mainApp.controller('EmployeeController'function ($scope) {  
  3.   
  4.     $scope.employee = {  
  5.   
  6.         firstName: "Santhakumar",  
  7.         lastName: "Munuswamy",  
  8.         salary: 10000,  
  9.   
  10.         fullName: function () {  
  11.   
  12.             var employeeObject;  
  13.   
  14.             employeeObject = $scope.employee;  
  15.   
  16.             return employeeObject.firstName + " " + employeeObject.lastName;  
  17.         }  
  18.     };  
  19.   
  20. });  

Steps 3: AngularJS Filters Application demo output as in the following screenshot,

 
 
 
Conclusion

This article helps you to understand the Filters with a sample application using Visual Studio 2015. Thank you for reading my articles. Kindly share your comments or suggestions.

Up Next
    Ebook Download
    View all
    Learn
    View all