AngularJS | Filters Last Updated : 11 Jun, 2019 Comments Improve Suggest changes Like Article Like Report There are some filters that are added in the AngularJS for the sake of making the formatting and working with data easier. There are several in-built filters in AngularJS. They are listed here along with some examples to make the understanding easier. Basic Syntax: Filters are generally added to the expressions by using the pipe (|) character. For example, the filter {{ fullName | uppercase }} formats the fullName into the uppercase format. Some of the pre-built filters in AngularJS are: currency The number is formatted to currency format. date The date is specified to a specific format. filter The array is filtered on the basis of the provided criteria. limitTo The array or an string is limited into a specified number of elements/characters. number A number if formatted to a string. orderBy The array is ordered by an expression. lowercase This filter converts a string to lowercase letters. uppercase This filter converts a string to uppercase letters. json It converts a JavaScript object into a JSON string. Currency Filter: This filter simply formats a number as currency. html <!DOCTYPE html> <html> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> </script> <body> <div ng-app="myApp" ng-controller="costCtrl"> <h1>Currency Format - GeeksforGeeks</h1> <h2>Price: {{ price | currency }}</h2> </div> <script> var app = angular.module('myApp', []); app.controller('costCtrl', function($scope) { $scope.price = 20; }); </script> </body> </html> Output: Here, the dollar ($) sign has been added automatically in front of the numerical value. Date Filter: The date filter formats a date to a specified format. Syntax: {{ date | date : format : timezone }} html <!DOCTYPE html> <html> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> </script> <body> <div ng-app="myApp" ng-controller="datCtrl"> <h1>GeeksforGeeks - Date Filter</h1> <p>Date = {{ today | date }}</p> </div> <script> var app = angular.module('myApp', []); app.controller('datCtrl', function($scope) { $scope.today = new Date(); }); </script> </body> </html> Output: Here, the date() function has been used and that displays the current date. Filter: This is used to display only the required objects. The filter selects a subset of an array. For example, This filter can be used only on arrays as this returns an array containing only the matching items(condition given in the array). html <!DOCTYPE html> <html> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> </script> <body> <div ng-app="myApp" ng-controller="namesCtrl"> <h1>filter - GeeksforGeeks</h1> <ul> <li ng-repeat="x in names | filter : 'e'"> {{ x }} </li> </ul> </div> <script> angular.module('myApp', []).controller('namesCtrl', function($scope) { $scope.names = [ 'Jani', 'Carl', 'Margareth', 'Hege', 'Joe', 'Gustav', 'Birgit', 'Mary', 'Kai' ]; }); </script> <p>This example displays only the names containing the letter "e".</p> </body> </html> Output: limitTo Filter: This filter returns an array or a string containing only a specified number of elements. The output will depend on the type of input that is given to the program. When used for arrays, it returns an array containing only the specified number of items. In the case of the string, it returns a string containing, only the specified number of characters, while when used for numbers, it returns a string containing only the specified number of digits. Syntax: {{ object | limitTo : limit : begin }} Here, limit specifies the number of elements to be displayed, while begin specifies where to begin the limitation from. html <!DOCTYPE html> <html> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> </script> <body> <div ng-app="myApp" ng-controller="sizeCtrl"> <h1>limitTo - GeeksforGeeks</h1> <ul> <li ng-repeat="x in cars | limitTo : 4 : 1">{{x}}</li> </ul> </div> <script> var app = angular.module('myApp', []); app.controller('sizeCtrl', function($scope) { $scope.cars = ["Audi", "BMW", "Dodge", "Fiat", "Ford", "Volvo", "Lamborghini"]; }); </script> <p>Filter applied from first element to the fifth element.</p> </body> </html> Output: orderBy Filter: This is used for sorting an array. Strings (default alphabetically) and numbers (default ascending) can be sorted using this filter. Syntax: {{ array | orderBy : expression : reverse }} Here, the reverse can be used to reverse the order of the resulting array. html <!DOCTYPE html> <html> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> </script> <body> <div ng-app="myApp" ng-controller="orderCtrl"> <h1>orderBy - GeeksforGeeks</h1> <ul> <li ng-repeat="x in customers | orderBy : 'city'"> {{x.name + ", " + x.city}} </li> </ul> </div> <script> var app = angular.module('myApp', []); app.controller('orderCtrl', function($scope) { $scope.customers = [{ "name": "Delhi" }, { "name": "Mumbai" }, { "name": "Patna" }, { "name": "Kolkata" }, { "name": "Pune" }, { "name": "Ranchi" }, { "name": "Bhopal" }]; }); </script> <p>Sorting in ascending order.</p> </body> </html> Output: number Filter: Probably the simplest filter. It simply formats a number to a string. Syntax: {{ string | number : fractionsize}} Here, 'fractionsize' specifies the number of decimals. html <!DOCTYPE html> <html> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> </script> <body> <div ng-app="myApp" ng-controller="nCtrl"> <h1>number Filter - GeeksforGeeks</h1> <h2>Rs.{{money | number : 3}}</h2> </div> <script> var app = angular.module('myApp', []); app.controller('nCtrl', function($scope) { $scope.money = 999999; }); </script> <p>The money is written with three decimals.</p> </body> </html> Output: lowercase Filter: This filter simply converts a string to lowercase letters. Syntax: {{ string | lowercase }} Let’s have a look at an example to get clear about this filter. html <!DOCTYPE html> <html> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> </script> <body> <h2>AngularJS - lowercase</h2> <br> <br> <div ng-app="myApp" ng-controller="myCtrl"> <strong>Input:</strong> <br> <input type="text" ng-model="string"> <br> <br> <strong>Output:</strong> <br> {{string|lowercase}} </div> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.string = ""; }); </script> </body> </html> The code mentioned above asks the user for an input. After the user enters a term in the input box, it gets stored in the ng-model=”string”. Now, the AngularJS will resolve the expression, and return the result exactly where the expression is written. AngularJS expressions can be written inside double braces, like this: {{ expression }}. Output: Here in this code, the output {{string}} is displayed just below the input box. However, to change the input string to lowercase, ‘|lowercase’ must be added to the expression’s name. Therefore, {{string|lowercase}} will return the input string in the lowercase format. uppercase Filter: The uppercase Filter in AngularJS is used to change a string to uppercase string or letters. Syntax: {{ string | uppercase}} html <!DOCTYPE html> <html> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> </script> <body> <div ng-app="myApp" ng-controller="caseCtrl"> <h1>{{txt | uppercase}}</h1> </div> <script> var app = angular.module('myApp', []); app.controller('caseCtrl', function($scope) { $scope.txt = "GeeksforGeeks!"; }); </script> <p>The text is written in uppercase letters.</p> </body> </html> Output: json Filter: This filter simply converts a JavaScript object into a JSON string, and this is very much useful while the debugging of applications. Syntax: {{ object | json : spacing }} Here, spacing specifies the number of spaces to use per indentation. The default value is 2, however, this value is optional. Have a look at this example code: html <!DOCTYPE html> <html> <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> </script> <body> <div ng-app="myApp" ng-controller="jsCtrl"> <h1>GeeksforGeeks</h1> <pre>{{customer | json : 20}}</pre> </div> <script> var app = angular.module('myApp', []); app.controller('jsCtrl', function($scope) { $scope.customer = { "name" : "Milk", "city" : "Patna", "country" : "India" }; }); </script> <p>A JSON string with 20 spaces per indentation.</p> </body> </html> Output: Let's have a look at the example of an array filter. html <!DOCTYPE html> <html> <body> <h1>GeeksforGeeks</h1> <p>Click the button to get every element in the array that has a value of 38 or more.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> var num = [23, 32, 56, 30, 56, 45, 34, 39]; function checkNum(num) { return num >= 38; } function myFunction() { document.getElementById( "demo").innerHTML = num.filter(checkNum); } </script> </body> </html> On clicking the 'Try it' button, all the elements in the array that has a value of 38 or more gets printed to the screen. Comment More info S sanchit496 Follow Improve Article Tags : Web Technologies AngularJS Explore AngularJS BasicsAngularJS Tutorial 5 min read Introduction to AngularJS 4 min read Angular CLI | Angular Project Setup 3 min read AngularJS Expressions 2 min read AngularJS Modules 3 min read AngularJS ng-model Directive 4 min read AngularJS Data Binding 4 min read AngularJS Controllers 3 min read AngularJS | Scope 2 min read AngularJS Services 4 min read AngularJS | AJAX - $http 3 min read AngularJS | Tables 2 min read AngularJS Select Boxes 2 min read AngularJS SQL 3 min read AngularJS HTML DOM 2 min read AngularJS Events 3 min read AngularJS | Forms 3 min read AngularJS Form Validation 3 min read AngularJS | API 2 min read AngularJS and W3.CSS 2 min read AngularJS Includes 3 min read AngularJS Animations 1 min read AngularJS | Application 3 min read AngularJS DirectivesAngularJS Directives 9 min read AngularJS ng-app Directive 1 min read AngularJS ng-bind Directive 2 min read AngularJS ng-bind-html Directive 2 min read AngularJS ng-bind-template Directive 2 min read AngularJS ng-blur Directive 1 min read AngularJS ng-change Directive 2 min read AngularJS ng-checked Directive 2 min read AngularJS ng-class Directive 2 min read AngularJS ng-class-even Directive 2 min read AngularJS ng-class-odd Directive 2 min read AngularJS ng-click Directive 2 min read AngularJS ng-cloak Directive 2 min read AngularJS ng-controller Directive 2 min read AngularJS Directives Complete Reference 2 min read AngularJS FiltersAngularJS | Filters 7 min read AngularJS currency Filter 2 min read AngularJS | date Filter 2 min read AngularJS filter Filter 3 min read AngularJS json Filter 2 min read AngularJS limitTo Filter 2 min read AngularJS lowercase Filter 1 min read AngularJS number Filter 1 min read AngularJS orderBy Filter 4 min read AngularJs uppercase Filter 1 min read AngularJS Converting FunctionsAngularJS angular.lowercase() Function 2 min read AngularJS angular.uppercase() Function 1 min read AngularJS angular.forEach() Function 1 min read AngularJS Comparing FunctionsAngularJS angular.isArray() Function 2 min read AngularJS angular.isDate() Function 2 min read AngularJS angular.isDefined() Function 2 min read AngularJS angular.isElement() Function 2 min read AngularJS angular.isFunction() Function 2 min read AngularJS angular.isNumber() Function 2 min read AngularJS angular.isObject() Function 2 min read AngularJS | angular.isString() Function 1 min read AngularJS angular.isUndefined() Function 2 min read AngularJS angular.equals() Function 2 min read AngularJS angular.toJson() Function 2 min read AngularJS QuestionsHow to bundle an Angular app for production? 4 min read How to add many functions in one ng-click directive? 2 min read How to directly update a field by using ng-click in AngularJS ? 3 min read How to Add Dynamic Options for Multiple Selects Inside ng-repeat Directive ? 3 min read How to detect when an @Input() value changes in Angular? 3 min read How to open popup using Angular and Bootstrap ? 2 min read How to reload or re-render the entire page using AngularJS? 2 min read How to add input fields dynamically on button click in AngularJS ? 2 min read How to Create Button Dynamically with Click Event in Angular ? 2 min read How to use jQuery in Angular ? 2 min read AngularJS Examples 2 min read Like