AngularJS Filters

You can add filters in AngularJS to format data.

AngularJS Filters

AngularJS provides filters to transform data:

  • currency Format a number into a currency format
  • date Format a date into a specified format
  • filter Select a subset from an array
  • json Format an object into a JSON string
  • limitTo Limit an array/string to a specified number of elements/characters
  • lowercase Format a string into lowercase
  • number Format a number into a string
  • orderBy Sort an array through an expression
  • uppercase Format a string into uppercase

Add filters to expressions

You can add filters to expressions using the pipe symbol | followed by a filter.

uppercase The filter formats a string into uppercase:

Instance

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

Try It Yourself

lowercase The filter formats a string into lowercase:

Instance

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

Try It Yourself

Add a filter to the directive

By using the pipe character | Followed by a filter, you can add a filter to the directive, for example ng-repeat:

Instance

The orderBy filter sorts the array:

<div ng-app="myApp" ng-controller="namesCtrl">
<ul>
  <li ng-repeat="x in names | orderBy:'country'">
    {{ x.name + ', ' + x.country }}
  </li>
</ul>
</div>

Try It Yourself

currency filter

currency The filter formats numbers as currency:

Instance

<div ng-app="myApp" ng-controller="costCtrl">
<h1>Price: {{ price | currency }}</h1>
</div>

Try It Yourself

For more information about the currency filter, please refer to our AngularJS currency filter reference manual.

filter filter

filter The filter selects a subset of the array.

filter The filter can only be used for arrays, it returns an array that only contains matching items.

Instance

Returns names containing the letter "i":

<div ng-app="myApp" ng-controller="namesCtrl">
<ul>
  <li ng-repeat="x in names | filter : 'i'">
    {{ x }}
  </li>
</ul>
</div>

Try It Yourself

For more information about the filter filter, please refer to our AngularJS filter filter reference manual.

Filter the array based on user input

By setting ng-model Instructions, we can use the value of the input field as an expression in the filter.

Enter a letter in the input field, and the list will be narrowed down/enlarged based on the matching items:

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

Instance

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

Try It Yourself

Sort the array based on user input

Click on the table header to change the sorting order:

Name Country Jani Norway Carl Sweden Margareth England Hege Norway Joe Denmark Gustav Sweden Birgit Denmark Mary England Kai Norway

By adding ng-click Instructions, we can run a function that will change the sorting order of the array:

Instance

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

Try It Yourself

Custom Filter

You can create your own filter by registering a new filter factory function with the module:

Instance

Create a custom filter named "myFormat":

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

Try It Yourself

myFormat Filters will format every other character to uppercase.