EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials AngularJS Tutorial AngularJS Filters
Secondary Sidebar
AngularJS Tutorial
  • AngularJS Basic and Advance
    • What is AngularJS
    • Career In AngularJS
    • Uses of Angular JS
    • AngularJS Architecture
    • Angular JS Application
    • AngularJS Application
    • AngularJS Filter Array
    • AngularJS ng-style
    • AngularJs ng init
    • AngularJs ng-repeat
    • AngularJs ng-change
    • AngularJs ng-options
    • AngularJs expressions
    • AngularJs onclick
    • AngularJS ng-show
    • AngularJs ng-include
    • Scope in AngularJS
    • AngularJS ng-if
    • AngularJS Number Filter
    • AngularJS Date Filter
    • AngularJS ng-class
    • AngularJS ng-model
    • AngularJS ng-disabled
    • AngularJS Custom Filter
    • AngularJS Validation
    • AngularJS Filters
    • Controllers in AngularJS
    • AngularJS Animations
    • AngularJS Services
    • AngularJS Events
    • AngularJS Versions
    • AngularJS Directives
    • AngularJS Unit Testing
    • AngularJS with Bootstrap
    • AngularJS UI Bootstrap
    • AngularJS Routing Example
  • Interview Question
    • Angular JS Interview Questions

AngularJS Filters

By Shalu SharmaShalu Sharma

AngularJS Filters

Introduction to AngularJS Filters

The filter is used to filter the elements. In other words filter in angular js is used to filter the element and object and returned the filtered items. Basically filter selects a subset from the array from the original array.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,883 ratings)

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

{{ arrayexpression | filter : expression : comparator : anyPropertyKey }}
  • comparator: This property is used to determining the value, it compares the expected value from the filtered expression and the actual value from the object array.
  • anyPropertyKey: This is a special property that is used to match the value against the given property. it has a default value as $.
  • arrayexpression: It takes the array on which we applied the filter.
  • expression: It is used to select the items from the array after the filter conditions are met.

Example of AngularJS Filters

Following is an example of angularjs filters:

<!DOCTYPE html>
<html>
<head>
<title>AngularJS | filter Filter</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body>
<div ng-app="myApp" ng-controller="namesCtrl">
<h1 style="color:green;">Demo filter</h1>
<ol>
<strong>
<li ng-repeat="x in names | filter : 'm'">
{{ x }}
</li>
</strong>
</ol>
</div&gt
<script>
angular.module('myApp', []).controller(
'namesCtrl', function($scope) {
$scope.names = [
'xyz',
'abc',
'uth',
'ert',
'opu',
'wrf',
'mkl',
'hgt',
'mnv'
];
});
</script>
<p>
This will show the names contain "m" character filter.
</p>
</body>
</html>

Output:

AngularJs Filters

List of the Filters available in AngularJS

Angular js provide many built-in filters which are listed below.

  • uppercase: This filter is used to format the string to upper case.
  • lowercase: This filter is used to format the string to lowercase.
  • currency: This filter is used to format a number to the current format.
  • orderBy: This filter is used to filter or order an array by an expression.
  • limitTo: This filter is used to limit the string/array into a specified length or number of elements or characters.
  • json: This filter is used to format the object to a JSON string.
  • filter: this filter selects the subset of items from an array.
  • date: This filter is used to format the date to the specified format.
  • number: This filter is used to format numbers to a string.

Examples to add filters to expression

Let us how to add filters to expression with the help of examples:

Example #1: Uppercase

Code:

<!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="personCtrl">
<p>Upper case example: {{ lastName | uppercase }}</p>
</div>
<script>
angular.module('myApp', []).controller('personCtrl', function($scope) {
$scope.firstName = "xyz",
$scope.lastName = "abc"
});
</script>
</body>
</html>

Output :

AngularJs Filters

Example #2: Lowercase

Code:

<!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="personCtrl">
<p>Lower case example: {{ lastName | lowercase }}</p>
</div>
<script>
angular.module('myApp', []).controller('personCtrl', function($scope) {
$scope.firstName = "XYZ",
$scope.lastName = "ABC"
});
</script>
</body>
</html>

Output :

AngularJs Filters

Example #3: currency

Code:

<!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="currencyCtrl">
<h1>Price: {{ price | currency }}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('currencyCtrl', function($scope) {
$scope.price = 60;
});
</script>
<p>Demo for currency filter. It will format a number to currency format</p>
</body>
</html>

Output :

AngularJs Filters

Examples to add filters to directives

Let us see how to add filters to directives with the help of examples:

Example #1: orderBy

Code:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<p>Demo to show orderBy : </p>
<div ng-app="myApp" ng-controller="nameandcountryCtrl">
<table border="1" width="100%">
<tr>
<th ng-click="orderByMe('nameEmp')">Name Emp</th>
<th ng-click="orderByMe('countryEmp')">Country Emp</th>
</tr>
<tr ng-repeat="x in names | orderBy:myOrderBy">
<td>{{x.nameEmp}}</td>
<td>{{x.countryEmp}}</td>
</tr>
</table>
</div>
<script>
angular.module('myApp', []).controller('nameandcountryCtrl', function($scope) {
$scope.names = [
{nameEmp:'anil',countryEmp:'England'},
{nameEmp:'sumit',countryEmp:'Sweden'},
{nameEmp:'arpita',countryEmp:'Norway'},
{nameEmp:'pankaj',countryEmp:'Norway'},
{nameEmp:'joe',countryEmp:'Denmark'},
{nameEmp:'aman',countryEmp:'Sweden'},
{nameEmp:'viman',countryEmp:'Denmark'},
{nameEmp:'manav',countryEmp:'England'},
{nameEmp:'Kapil',countryEmp:'Netherland'}
];
$scope.orderByMe = function(x) {
$scope.myOrderBy = x;
}
});
</script>
</body>
</html>

Output :

orderBy Example

Example #2: LimtTo

Code:

<!DOCTYPE html>
<html>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
<h2>Demo LimitTo </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|limitTo:8}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.string = "";
});
</script>
</body>
</html>

Output:

AngularJs Filters

Example #3: Json

Code:

<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body>
<div ng-app="result" ng-controller="resultCtrl">
<h1>JSON: To show name and salary</h1>
<pre>{{names | json}}</pre>
</div>
<script>
var app = angular.module('result', []);
app.controller('resultCtrl', function($scope) {
$scope.names = {
"Aman" : 356,
"Vijay" : 908,
"Pamkaj" : 645,
"Chinmay" : 195,
"Joe" : 740
};
});
</script>
</body>
</html>

Output :

json Example

Example #4: date

When we do not specify the date format it takes the default format as ‘MMM d, yyyy’. Here time zone parameter is optional. takes two parameter format and time zone.

Syntax:

{{ date | date : format : timezone }}

Some predefined date format is as follows:

  • “medium time”: Equivalent to “h:mm:ss a” (2:35:05 AM)
  • “short date”: Equivalent to “M/d/yy” (5/7/19)
  • “medium”: Equivalent to “MMM d, y h:mm: ss a”
  • “long date”: Equivalent to “MMMM d, y” (May 7, 2019)
  • “short”: Equivalent to “M/d/yy h: mm a”
  • “short-time”: Equivalent to “h: mm a” (2:35 AM)
  • “full date”: Equivalent to “EEEE, MMMM d, y” (Tuesday, May 7, 2019)
  • “medium date”: Equivalent to “MMM d, y” (May 7, 2019)

Code:

<!DOCTYPE html>
<html>
<head>
<title>Date Filter to show current date</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body>
<h5>Date Filter to show current date</h5>
<div ng-app="gfgApp" ng-controller="dateCntrl">
<p>{{ today | date : "dd.MM.y" }}</p>
</div>
<script>
var app = angular.module('gfgApp', []);
app.controller('dateCntrl', function($scope) {
$scope.today = new Date();
});
</script>
</body>
</html>

Output :

Date filter

Example #5: number

code:

{{ string| number : fractionSize}}
<!DOCTYPE html>
<html>
<head>
<title>Number Filter</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body>
<div ng-app="gfgApp" ng-controller="numberCntrl">
<h3>Number filter demo</h3>
<p>Number : {{ value| number  : 2}}</p>
</div>
<script>
var app = angular.module('gfgApp', []);
app.controller('numberCntrl', function($scope) {
$scope.value = 75560.569;
});
</script>
</body>
</html>

Output:

number Example

Advantages of filters in AngularJs

Angular js provide us many built-in filters to filter our data according to our needs. But there is own more advantage that we can create our own custom filters.

Filters are used to manipulate our data. They provide faster processing and hence enhance the performance as well. Filters are very robust as well.

Conclusion

We can use filters anywhere in our angular application like a directive, expression, controller, etc. We can also create custom filters according to which property we want to sort our data. It also reduces code and makes it more readable and optimizes and by creating custom filters we can maintain them into separate files.

Recommended Articles

This is a guide to the AngularJS Filters. Here we discuss the introduction, list of the filters available in AngularJS, and the advantages along with appropriate examples. You may also look at the following articles to learn more –

  1. AngularJS Directives
  2. Uses of Angular JS
  3. Angular Alternatives
  4. AngularJS Validation
Popular Course in this category
Angular JS Training Program (9 Courses, 7 Projects)
  9 Online Courses |  7 Hands-on Projects |  64+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more