SlideShare a Scribd company logo
Filters 
In AngularJS, a filter provides a way to format the data we display to the user. Angular gives us 
Several built-in filters as well as an easy way to create our own. 
Filter can be used in view templates, controllers or services and it is easy to define your own custom filter. 
We invoke filters in our HTML with the | (pipe) character inside the template binding characters {{ 
}}. 
Example:- 
{{ Value goes here | Filter name goes here }} 
Build in filter in angularjs 
Name Description 
filter Selects a subset of items from array and returns it as a new array. 
currency 
Formats a number as a currency (ie $1,234.56). When no currency symbol is provided, default symbol for 
current locale is used. 
number Formats a number as text. 
date Formats date to a string based on the requested format. 
json Allows you to convert a JavaScript object into JSON string. 
lowercase Converts string to lowercase. 
uppercase Converts string to uppercase. 
limitTo 
Creates a new array or string containing only a specified number of elements. The elements are taken 
from either the beginning or the end of the source array, string or number, as specified by the value and 
sign (positive or negative) of limit. If a number is used as input, it is converted to a string. 
orderBy 
Orders a specified array by the expression predicate. It is ordered alphabetically for strings and 
numerically for numbers. Note: if you notice numbers are not being sorted corre ctly, make sure they are 
actually being saved as numbers and not strings. 
external.js 
//defining module 
var app = angular.module('IG', []) 
app.controller('FirstController', ['$scope', function ($scope) { 
$scope.customers = [ 
{ 
name: 'David', 
street: '1234 Anywhere St.' 
}, 
{
name: 'Tina', 
street: '1800 Crest St.' 
}, 
{ 
name: 'Brajesh', 
street: 'Noida' 
}, 
{ 
name: 'Michelle', 
street: '890 Main St.' 
} 
]; 
} ]) 
Index.html 
<!DOCTYPE html> 
<html ng-app="IG"> 
<head lang="en"> 
<meta charset="UTF-8"> 
<title>Filter in AngularJS</title> 
<script 
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"> 
</script> 
<script src="Scripts/external.js"></script> 
</head> 
<body> 
<div ng-controller="FirstController"> 
<input type="text" ng-model="SearchData" /> 
<ul> 
<li ng-repeat="Cust in customers | filter:SearchData | orderBy:'name' | limitTo:2"> 
{{Cust.name}} - {{Cust.street}} 
</li> 
</ul> 
<p>Filter uppercase ::- {{ "I am Done" | uppercase }}</p> 
<p>Filter lowercase ::- {{ "Are you Done with your Work" | lowercase }}</p> 
<p>Filter currency ::- {{'8794'|currency}}</p> 
<p>Filter format Number ::- {{'871234'|number}}</p> 
</div> 
</body> 
</html> 
For instance, let’s say we want to capitalize our string. We can either change all the characters 
In a string to be capitalized, or we can use a filter. 
We can also use filters from within JavaScript by using the $filter service. 
For instance, to use the lowercase JavaScript filter: 
app.controller('DemoController', ['$scope', '$filter', 
function ($scope, $filter) { 
$scope.name = $filter('lowercase')('Ari'); 
}]);
Making Our Own Filter 
As we saw above, it’s really easy to create our own custom filter. To create a filter, we put it under 
Its own module. Let’s create one together: a filter that capitalizes the first character of a string. 
First, we need to create it in a module that we’ll require in our app (this step is good practice): 
//defining module 
var app = angular.module('IG', []) 
//define filter 
app.filter('capitalize', function () { 
return function (input) { } 
}); 
//define filter using service as a dependent 
app.filter('ReverseData', function (data) { 
return function (Message) { 
return Message.split("").reverse().join("") + data.Message; 
}; 
}) 
Filters are just functions to which we pass input. In the function above, we simply take the input as 
the string on which we are calling the filter. We can do some error checking inside the function: 
//defining module 
var app = angular.module('IG', []) 
//define filter 
app.filter('capitalize', function () { 
return function (input) { 
// input will be the string we pass in 
if (input) 
return input[0].toUpperCase() + input.slice(1); 
} 
}); 
Now, if we want to capitalize the first letter of a sentence, we can first lowercase the entire string and then capitalize 
the first letter with our filter: 
<!-- Ginger loves dog treats --> 
{{ 'ginger loves dog treats' | lowercase | capitalize }} 
Custom Filter Example 
Index.html 
<!DOCTYPE html> 
<html ng-app="IG"> 
<head lang="en"> 
<meta charset="UTF-8"> 
<title>Custom Filter in AngularJS</title>
<script 
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"> 
</script> 
<script src="Scripts/external.js"></script> 
</head> 
<body> 
<div ng-controller="FirstController"> 
<p>{{name}}</p> 
<p>{{"i am brajesh" | LabelMessage}}</p> 
<p>{{"i am brajesh" | ReverseData}}</p> 
<p> 
{{Amount | changevalue:25:3}} 
</p> 
</div> 
</body> 
</html> 
external.js 
//defining module 
var app = angular.module('IG', []) 
app.controller('FirstController', ['$scope','$filter', function ($scope,$filter) { 
$scope.name = $filter('uppercase')('I am done with Controller filter'); 
$scope.Amount = 8000.78; 
} 
]); 
//Filter used for welcome Message 
app.filter('LabelMessage', function () { 
return function (input) { 
if (input) 
return "Welcome "+ input[0].toUpperCase() + input.slice(1); 
} 
}); 
//Filter is used to reverse string data 
app.filter('ReverseData', function () { 
return function (Message) { 
return Message.split("").reverse().join(""); 
}; 
}) 
//Pass multiple value into Custom Filter 
app.filter('changevalue', function () { 
return function (value, discount, DP) { 
var Amount = value; 
value = Amount * discount / 100; 
return value.toFixed(DP); 
}; 
});

More Related Content

DOCX
Built in filters
DOCX
Controller in AngularJS
PPTX
AngularJS Introduction
DOCX
Directives
DOCX
Understanding angular js $rootscope and $scope
DOCX
Shaping up with angular JS
PPTX
AngularJS Directives
PPTX
Understanding angular js
Built in filters
Controller in AngularJS
AngularJS Introduction
Directives
Understanding angular js $rootscope and $scope
Shaping up with angular JS
AngularJS Directives
Understanding angular js

What's hot (20)

PPTX
AngularJS in 60ish Minutes
PDF
AngularJS Basic Training
PPTX
AngularJs
PPTX
Why angular js Framework
PDF
AngularJS Basics with Example
PPTX
Practical AngularJS
PPTX
Angular js
PPTX
Angular directive filter and routing
PPTX
Building an End-to-End AngularJS Application
PDF
AngularJS Framework
PPTX
AngularJs presentation
PDF
Angular js routing options
PPTX
Angular js architecture (v1.4.8)
PPTX
Angular JS - Introduction
PDF
Advanced Tips & Tricks for using Angular JS
ODP
AngularJs Crash Course
PDF
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
PPTX
Jquery Complete Presentation along with Javascript Basics
PPTX
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
PDF
Introduction to AngularJS
AngularJS in 60ish Minutes
AngularJS Basic Training
AngularJs
Why angular js Framework
AngularJS Basics with Example
Practical AngularJS
Angular js
Angular directive filter and routing
Building an End-to-End AngularJS Application
AngularJS Framework
AngularJs presentation
Angular js routing options
Angular js architecture (v1.4.8)
Angular JS - Introduction
Advanced Tips & Tricks for using Angular JS
AngularJs Crash Course
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Jquery Complete Presentation along with Javascript Basics
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Introduction to AngularJS
Ad

Similar to Filters in AngularJS (20)

PPTX
Angular Presentation
PPTX
AngularJS
PDF
AngularJS Basics
DOCX
Angular js
PPTX
Angular workshop - Full Development Guide
PPTX
Javascripting.pptx
PDF
Angular.js Primer in Aalto University
PPT
AngularJS Mobile Warsaw 20-10-2014
PPTX
Angular js
PPTX
Angular js
PDF
Introduction to angular js
PPTX
Basics of AngularJS
PPTX
Angular Js Get Started - Complete Course
PPTX
angularJs Workshop
PPTX
Angular
PPTX
Angular
ODP
Scal`a`ngular - Scala and Angular
PPTX
AngularJs Workshop SDP December 28th 2014
PPTX
Method and decorator
PDF
Working with Javascript in Rails
Angular Presentation
AngularJS
AngularJS Basics
Angular js
Angular workshop - Full Development Guide
Javascripting.pptx
Angular.js Primer in Aalto University
AngularJS Mobile Warsaw 20-10-2014
Angular js
Angular js
Introduction to angular js
Basics of AngularJS
Angular Js Get Started - Complete Course
angularJs Workshop
Angular
Angular
Scal`a`ngular - Scala and Angular
AngularJs Workshop SDP December 28th 2014
Method and decorator
Working with Javascript in Rails
Ad

Recently uploaded (20)

PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
master seminar digital applications in india
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
01-Introduction-to-Information-Management.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Pharma ospi slides which help in ospi learning
O5-L3 Freight Transport Ops (International) V1.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Renaissance Architecture: A Journey from Faith to Humanism
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
RMMM.pdf make it easy to upload and study
Microbial disease of the cardiovascular and lymphatic systems
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Anesthesia in Laparoscopic Surgery in India
master seminar digital applications in india
O7-L3 Supply Chain Operations - ICLT Program
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
VCE English Exam - Section C Student Revision Booklet
01-Introduction-to-Information-Management.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Institutional Correction lecture only . . .
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES

Filters in AngularJS

  • 1. Filters In AngularJS, a filter provides a way to format the data we display to the user. Angular gives us Several built-in filters as well as an easy way to create our own. Filter can be used in view templates, controllers or services and it is easy to define your own custom filter. We invoke filters in our HTML with the | (pipe) character inside the template binding characters {{ }}. Example:- {{ Value goes here | Filter name goes here }} Build in filter in angularjs Name Description filter Selects a subset of items from array and returns it as a new array. currency Formats a number as a currency (ie $1,234.56). When no currency symbol is provided, default symbol for current locale is used. number Formats a number as text. date Formats date to a string based on the requested format. json Allows you to convert a JavaScript object into JSON string. lowercase Converts string to lowercase. uppercase Converts string to uppercase. limitTo Creates a new array or string containing only a specified number of elements. The elements are taken from either the beginning or the end of the source array, string or number, as specified by the value and sign (positive or negative) of limit. If a number is used as input, it is converted to a string. orderBy Orders a specified array by the expression predicate. It is ordered alphabetically for strings and numerically for numbers. Note: if you notice numbers are not being sorted corre ctly, make sure they are actually being saved as numbers and not strings. external.js //defining module var app = angular.module('IG', []) app.controller('FirstController', ['$scope', function ($scope) { $scope.customers = [ { name: 'David', street: '1234 Anywhere St.' }, {
  • 2. name: 'Tina', street: '1800 Crest St.' }, { name: 'Brajesh', street: 'Noida' }, { name: 'Michelle', street: '890 Main St.' } ]; } ]) Index.html <!DOCTYPE html> <html ng-app="IG"> <head lang="en"> <meta charset="UTF-8"> <title>Filter in AngularJS</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"> </script> <script src="Scripts/external.js"></script> </head> <body> <div ng-controller="FirstController"> <input type="text" ng-model="SearchData" /> <ul> <li ng-repeat="Cust in customers | filter:SearchData | orderBy:'name' | limitTo:2"> {{Cust.name}} - {{Cust.street}} </li> </ul> <p>Filter uppercase ::- {{ "I am Done" | uppercase }}</p> <p>Filter lowercase ::- {{ "Are you Done with your Work" | lowercase }}</p> <p>Filter currency ::- {{'8794'|currency}}</p> <p>Filter format Number ::- {{'871234'|number}}</p> </div> </body> </html> For instance, let’s say we want to capitalize our string. We can either change all the characters In a string to be capitalized, or we can use a filter. We can also use filters from within JavaScript by using the $filter service. For instance, to use the lowercase JavaScript filter: app.controller('DemoController', ['$scope', '$filter', function ($scope, $filter) { $scope.name = $filter('lowercase')('Ari'); }]);
  • 3. Making Our Own Filter As we saw above, it’s really easy to create our own custom filter. To create a filter, we put it under Its own module. Let’s create one together: a filter that capitalizes the first character of a string. First, we need to create it in a module that we’ll require in our app (this step is good practice): //defining module var app = angular.module('IG', []) //define filter app.filter('capitalize', function () { return function (input) { } }); //define filter using service as a dependent app.filter('ReverseData', function (data) { return function (Message) { return Message.split("").reverse().join("") + data.Message; }; }) Filters are just functions to which we pass input. In the function above, we simply take the input as the string on which we are calling the filter. We can do some error checking inside the function: //defining module var app = angular.module('IG', []) //define filter app.filter('capitalize', function () { return function (input) { // input will be the string we pass in if (input) return input[0].toUpperCase() + input.slice(1); } }); Now, if we want to capitalize the first letter of a sentence, we can first lowercase the entire string and then capitalize the first letter with our filter: <!-- Ginger loves dog treats --> {{ 'ginger loves dog treats' | lowercase | capitalize }} Custom Filter Example Index.html <!DOCTYPE html> <html ng-app="IG"> <head lang="en"> <meta charset="UTF-8"> <title>Custom Filter in AngularJS</title>
  • 4. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"> </script> <script src="Scripts/external.js"></script> </head> <body> <div ng-controller="FirstController"> <p>{{name}}</p> <p>{{"i am brajesh" | LabelMessage}}</p> <p>{{"i am brajesh" | ReverseData}}</p> <p> {{Amount | changevalue:25:3}} </p> </div> </body> </html> external.js //defining module var app = angular.module('IG', []) app.controller('FirstController', ['$scope','$filter', function ($scope,$filter) { $scope.name = $filter('uppercase')('I am done with Controller filter'); $scope.Amount = 8000.78; } ]); //Filter used for welcome Message app.filter('LabelMessage', function () { return function (input) { if (input) return "Welcome "+ input[0].toUpperCase() + input.slice(1); } }); //Filter is used to reverse string data app.filter('ReverseData', function () { return function (Message) { return Message.split("").reverse().join(""); }; }) //Pass multiple value into Custom Filter app.filter('changevalue', function () { return function (value, discount, DP) { var Amount = value; value = Amount * discount / 100; return value.toFixed(DP); }; });