AngularJS Filters
- Previous Page AngularJS Scope
- Next Page AngularJS Services
You can add filters in AngularJS to format data.
AngularJS Filters
AngularJS provides filters to transform data:
currency
Format a number into a currency formatdate
Format a date into a specified formatfilter
Select a subset from an arrayjson
Format an object into a JSON stringlimitTo
Limit an array/string to a specified number of elements/characterslowercase
Format a string into lowercasenumber
Format a number into a stringorderBy
Sort an array through an expressionuppercase
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>
lowercase
The filter formats a string into lowercase:
Instance
<div ng-app="myApp" ng-controller="personCtrl"> <p>The name is {{ lastName | lowercase }}</p> </div>
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>
currency filter
currency
The filter formats numbers as currency:
Instance
<div ng-app="myApp" ng-controller="costCtrl"> <h1>Price: {{ price | currency }}</h1> </div>
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>
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>
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 NorwayBy 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>
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>
myFormat
Filters will format every other character to uppercase.
- Previous Page AngularJS Scope
- Next Page AngularJS Services