SlideShare une entreprise Scribd logo
1  sur  4
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); 
}; 
});

Contenu connexe

Tendances

Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
Wei Ru
 

Tendances (20)

AngularJS in 60ish Minutes
AngularJS in 60ish MinutesAngularJS in 60ish Minutes
AngularJS in 60ish Minutes
 
AngularJS Basic Training
AngularJS Basic TrainingAngularJS Basic Training
AngularJS Basic Training
 
AngularJs
AngularJsAngularJs
AngularJs
 
Why angular js Framework
Why angular js Framework Why angular js Framework
Why angular js Framework
 
AngularJS Basics with Example
AngularJS Basics with ExampleAngularJS Basics with Example
AngularJS Basics with Example
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
 
Angular js
Angular jsAngular js
Angular js
 
Angular directive filter and routing
Angular directive filter and routingAngular directive filter and routing
Angular directive filter and routing
 
Building an End-to-End AngularJS Application
Building an End-to-End AngularJS ApplicationBuilding an End-to-End AngularJS Application
Building an End-to-End AngularJS Application
 
AngularJS Framework
AngularJS FrameworkAngularJS Framework
AngularJS Framework
 
AngularJs presentation
AngularJs presentation AngularJs presentation
AngularJs presentation
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
 
Angular js architecture (v1.4.8)
Angular js architecture (v1.4.8)Angular js architecture (v1.4.8)
Angular js architecture (v1.4.8)
 
Angular JS - Introduction
Angular JS - IntroductionAngular JS - Introduction
Angular JS - Introduction
 
Advanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JSAdvanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JS
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosAngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 

Similaire à Filters in AngularJS

Angular Presentation
Angular PresentationAngular Presentation
Angular Presentation
Adam Moore
 

Similaire à Filters in AngularJS (20)

Angular Presentation
Angular PresentationAngular Presentation
Angular Presentation
 
AngularJS
AngularJSAngularJS
AngularJS
 
AngularJS Basics
AngularJS BasicsAngularJS Basics
AngularJS Basics
 
Angular js
Angular jsAngular js
Angular js
 
Angular workshop - Full Development Guide
Angular workshop - Full Development GuideAngular workshop - Full Development Guide
Angular workshop - Full Development Guide
 
Javascripting.pptx
Javascripting.pptxJavascripting.pptx
Javascripting.pptx
 
Angular.js Primer in Aalto University
Angular.js Primer in Aalto UniversityAngular.js Primer in Aalto University
Angular.js Primer in Aalto University
 
AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014
 
Angular js
Angular jsAngular js
Angular js
 
Angular js
Angular jsAngular js
Angular js
 
Introduction to angular js
Introduction to angular jsIntroduction to angular js
Introduction to angular js
 
Basics of AngularJS
Basics of AngularJSBasics of AngularJS
Basics of AngularJS
 
Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete Course
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs Workshop
 
Angular
AngularAngular
Angular
 
Angular
AngularAngular
Angular
 
Scal`a`ngular - Scala and Angular
Scal`a`ngular - Scala and AngularScal`a`ngular - Scala and Angular
Scal`a`ngular - Scala and Angular
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014
 
Method and decorator
Method and decoratorMethod and decorator
Method and decorator
 
Working with Javascript in Rails
Working with Javascript in RailsWorking with Javascript in Rails
Working with Javascript in Rails
 

Dernier

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 

Dernier (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

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); }; });