Differentiation of AngularJS Filters

If you are interested to learn about the AngularJS Scope

Filters can be added in AngularJS to format data. AngularJS Filters allow us to format the data to display on UI without changing original format. Filters can be used with an expression or directives using pipe | sign. {{expression | filterName:parameter }} Angular includes various filters to format data of different data types.

AngularJS Filters

AngularJS provides filters to transform data:

  • currency Format a number to a currency format.
  • date Format a date to a specified format.
  • filter Select a subset of items from an array.
  • json Format an object to a JSON string.
  • limitTo Limits an array/string, into a specified number of elements/characters.
  • lowercase Format a string to lower case.
  • number Format a number to a string.
  • orderBy Orders an array by an expression.
  • uppercase Format a string to upper case.

Adding Filters to Expressions

Filters can be added to expressions by using the pipe character |, followed by a filter. The uppercase filter format strings to upper case:


<div ng-app="myApp" ng-controller="personCtrl"> <
p>The name is {{ lastName | uppercase }}</p>  </div>

The lowercase filter format strings to lower case:


<div ng-app="myApp" ng-controller="personCtrl"> <
p>The name is {{ lastName | lowercase }}</p>n </div>

Adding Filters to Directives

Filters are added to directives, like ng-repeat, by using the pipe character |, followed by a filter:


The orderBy filter sorts an array:<div ng-app=”myApp” ng-controller=”namesCtrl”>

&lt;ul><br>  &lt;li ng-repeat="x in names | orderBy:
'country'"><br>    {{ x.name + ', ' + x.country }}<br>  &lt;
/li><br>&lt;/ul> &lt;/div>

Read more about the currency filter in our AngularJS currency Filter Reference

When to use a filter in AngularJS?

When we want to display the data in view part in a particular format in that scenario we can use AngularJS filter. We can display the data in the uppercase format, lowercase format etc. In whatever format, we entered the text but it can easily get displayed in any format by angular as per the type of filter used. AngularJS Filters can change the way of displaying the output.

AngularJS Filter in View Template

Filters are applied on expression to refrain the input. We can apply the AngualrJS Filter in a view template. Also, we can apply a filter on the result obtained by another filter. Chaining is the phenomenon when filters are applied to the already filtered content.


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

AngularJS Filters have arguments.


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

When will filter get execute. Whenever the inputs have changed in the template, the filter gets to execute.

Filter an Array Based on User Input

By setting the ng-model directive on an input field, we can use the value of the input field as an expression in a filter. Type a letter in the input field, and the list will shrink/grow depending on the match:

  • Jani
  • Carl
  • Margareth
  • Hege
  • Joe
  • Gustav
  • Birgit
  • Mary
  • Kai


&lt;div ng-app="myApp" ng-controller="namesCtrl"> &lt;p>&lt;
input type="text" ng-model="test">&lt;/p> &lt;ul><br>  &lt;
li ng-repeat="x in names | filter : test"><br>    {{ x }}<br>  &lt;/li><br>&lt;/ul> &lt;/div>

What is filtering in angular?

Filter is an important part in AngularJS as well as Angular 2 or Angular 4. It is basically used to filter an item from a group of items, which are there in an array or an object array. It selects a subset of the items from an array and returns it as a new array and this item is displayed on UI.

Sort an Array Based on User Input

Click the table headers to change the sort order::


By adding the ng-click directive on the table headers, we can run a function that changes the sorting order of the array:

AngularJS Filter - Built-in, Custom and Stateful Filters With Examples -  DataFlair


&lt;div ng-app="myApp" ng-controller="namesCtrl"> &lt;
table border="1" width="100%"><br>  &lt;tr><br>    &lt;
th ng-click="orderByMe('name')">Name&lt;/th><br>    &lt;
th ng-click="orderByMe('country')">Country&lt;/th><br>  &lt;
/tr><br>  &lt;tr ng-repeat="x in names | orderBy:myOrderBy"><br>    &lt;
td>{{x.name}}&lt;/td><br>    &lt;td>{{x.country}}&lt;/td><br>  &lt;/tr><br>&lt;
/table> &lt;
/div> &lt;script><br>angular.module('myApp', []).controller('namesCtrl',
 function($scope) {<br>  $scope.names = [<br>    {name:'Jani',country:'Norway'},<br>    {name:'Carl',country:'Sweden'},<br>    {name:'Margareth',country:'England'},<br>    {name:'Hege',country:'Norway'},<br>    {name:'Joe',country:'Denmark'},<br>    {name:'Gustav',country:'Sweden'},<br>    {name:'Birgit',country:'Denmark'},<br>    {name:'Mary',country:'England'},<br>    {name:'Kai',country:'Norway'}<br>  ];<br>  $scope.orderByMe = function(x) {<br>    $scope.myOrderBy = x;<br>  }<br>});<br>&lt;/script>

Custom Filters

You can make your own filters by registering a new filter factory function with your module:


Make a custom filter called "myFormat":&lt;
ul ng-app="myApp" ng-controller="namesCtrl"><br>  &lt;
li ng-repeat="x in names"><br>    {{x | <strong>myFormat</strong>}}<br>  &lt;
/li><br>&lt;/ul> &lt;script><br>var app = angular.module('myApp', []);<br>app.
filter('<strong>myFormat</strong>', function() {<br>  return function(x) {<br>    var i, c, txt = "";
<br>    for (i = 0; i &lt; x.length; i++) {<br>      c = x[i];<br>      if (i % 2 == 0) {<br>        c = c.toUpperCase();<br>      }<br>      txt += c;<br>    }<br>    return txt;<br>  };
<br>});<br>app.controller('namesCtrl', function($scope) {<br>  $scope.names = ['Jani', 'Carl', 'Margareth', 'Hege', 'Joe', 'Gustav', 'Birgit', 'Mary', 'Kai'];<br>});&lt;/script>

Filters are used to modify the data. They can be clubbed in expression or directives using pipe (|) character. The following list shows the commonly used filters.

Sr.No.Name & Description
1uppercaseconverts a text to upper case text.
2lowercaseconverts a text to lower case text.
3currencyformats text in a currency format.
4filterfilter the array to a subset of it based on provided criteria.
5orderbyorders the array based on provided criteria.

Uppercase Filter

Add uppercase filter to an expression using pipe character. Here we’ve added uppercase filter to print student name in all capital letters.

Enter first name:&lt;input type = "text" ng-model = "student.firstName"&gt;
Enter last name: &lt;input type = "text" ng-model = "student.lastName"&gt;
Name in Upper Case: {{student.fullName() | uppercase}}

Lowercase Filter

Add lowercase filter to an expression using pipe character. Here we’ve added lowercase filter to print student name in all lowercase letters.

Enter first name:&lt;input type = "text" ng-model = "student.firstName"&gt;
Enter last name: &lt;input type = "text" ng-model = "student.lastName"&gt;
Name in Lower Case: {{student.fullName() | lowercase}}

Currency Filter

Add currency filter to an expression returning number using pipe character. Here we’ve added currency filter to print fees using currency format.

Enter fees: &lt;input type = "text" ng-model = "student.fees"&gt;
fees: {{student.fees | currency}}


To display only required subjects, we use subjectName as filter.

Enter subject: &lt;input type = "text" ng-model = "subjectName"&gt;
   &lt;li ng-repeat = "subject in student.subjects | filter: subjectName"&gt;
      {{ subject.name + ', marks:' + subject.marks }}

OrderBy Filter

To order subjects by marks, we use orderBy marks.

   &lt;li ng-repeat = "subject in student.subjects | orderBy:'marks'"&gt;
      {{ subject.name + ', marks:' + subject.marks }}


The following example shows use of all the above mentioned filters.


      &lt;title&gt;Angular JS Filters&lt;/title&gt;
      &lt;script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"&gt;
      &lt;h2&gt;AngularJS Sample Application&lt;/h2&gt;
      &lt;div ng-app = "mainApp" ng-controller = "studentController"&gt;
         &lt;table border = "0"&gt;
               &lt;td&gt;Enter first name:&lt;/td&gt;
               &lt;td&gt;&lt;input type = "text" ng-model = "student.firstName"&gt;&lt;/td&gt;
               &lt;td&gt;Enter last name: &lt;/td&gt;
               &lt;td&gt;&lt;input type = "text" ng-model = "student.lastName"&gt;&lt;/td&gt;
               &lt;td&gt;Enter fees: &lt;/td&gt;
               &lt;td&gt;&lt;input type = "text" ng-model = "student.fees"&gt;&lt;/td&gt;
               &lt;td&gt;Enter subject: &lt;/td&gt;
               &lt;td&gt;&lt;input type = "text" ng-model = "subjectName"&gt;&lt;/td&gt;
         &lt;table border = "0"&gt;
               &lt;td&gt;Name in Upper Case: &lt;/td&gt;&lt;td&gt;{{student.fullName() | uppercase}}&lt;/td&gt;
               &lt;td&gt;Name in Lower Case: &lt;/td&gt;&lt;td&gt;{{student.fullName() | lowercase}}&lt;/td&gt;
               &lt;td&gt;fees: &lt;/td&gt;&lt;td&gt;{{student.fees | currency}}
                     &lt;li ng-repeat = "subject in student.subjects | filter: subjectName |orderBy:'marks'"&gt;
                        {{ subject.name + ', marks:' + subject.marks }}
         var mainApp = angular.module("mainApp", []);
         mainApp.controller('studentController', function($scope) {
            $scope.student = {
               firstName: "Mahesh",
               lastName: "Parashar",
               fullName: function() {
                  var studentObject;
                  studentObject = $scope.student;
                  return studentObject.firstName + " " + studentObject.lastName;

Differentiation of AngularJS Filters
Show Buttons
Hide Buttons