SlideShare ist ein Scribd-Unternehmen logo
1 von 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); 
}; 
});

Weitere ähnliche Inhalte

Was ist angesagt?

Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
Wei Ru
 

Was ist angesagt? (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
 

Ähnlich wie Filters in AngularJS

Angular Presentation
Angular PresentationAngular Presentation
Angular Presentation
Adam Moore
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 

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