AngularJS Filters


Filters can be added to expressions and directives using a pipe character (|).


AngularJS Filters

AngularJS filters can be used to transform data:

Filter Description
currency Formats a number into currency format.
filter Selects a subset from array items.
lowercase Formats a string to lowercase.
orderBy Sorts an array according to an expression.
uppercase Formats a string to uppercase.

Add a Filter in Expressions

Filters can be added to expressions using a pipe character (|) and a filter.

(In the two examples below, we will use the person controller mentioned in the previous chapter)

uppercaseThe filter formats a string to uppercase:

AngularJS Example

<div ng-app="myApp" ng-controller="personCtrl">

<p>The name is{{ lastName | uppercase }}</p>

</div>

Try it Yourself »

lowercaseThe filter formats a string to lowercase:

AngularJS Example

<div ng-app="myApp" ng-controller="personCtrl">

<p>The name is{{ lastName | lowercase }}</p>

</div>

Try it Yourself »

currency Filter

currencyThe filter formats a number into currency format:

AngularJS Example

<div ng-app="myApp" ng-controller="costCtrl">

<input type="number" ng-model="quantity">
<input type="number" ng-model="price">

<p>Total price ={{ (quantity * price) | currency }}</p>

</div>

Try it Yourself »

Add a Filter to Directives

Filters can be added to directives using a pipe character (|) and a filter.

orderByThe filter sorts an array according to an expression:

AngularJS Example

<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 »

Filter Input

The input filter can be added to a directive using a pipe character (|) and a filter, followed by a colon and a model name.

filterThe filter selects a subset from an array:

AngularJS Example

<div ng-app="myApp" ng-controller="namesCtrl">

<p><input type="text" ng-model="test"></p>

<ul>
  <li ng-repeat="x in names | filter:test | orderBy:'country'">
    {{ (x.name | uppercase) + ', ' + x.country }}
  </li>
</ul>

</div>

Try it Yourself »

Custom Filter

The following example defines a custom filterreversethat reverses a string:

AngularJS Example

var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.msg = "Example"; }); app.filter('reverse', function() { //You can inject dependencies return function(text) { return text.split("").reverse().join(""); } });

Try it Yourself »
Other Extensions