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

What's hot

AngularJS in 60ish Minutes
AngularJS in 60ish MinutesAngularJS in 60ish Minutes
AngularJS in 60ish Minutes
Dan Wahlin
 
AngularJS Basic Training
AngularJS Basic TrainingAngularJS Basic Training
AngularJS Basic Training
Cornel Stefanache
 
AngularJs
AngularJsAngularJs
AngularJs
syam kumar kk
 
Why angular js Framework
Why angular js Framework Why angular js Framework
Why angular js Framework
Sakthi Bro
 
AngularJS Basics with Example
AngularJS Basics with ExampleAngularJS Basics with Example
AngularJS Basics with Example
Sergey Bolshchikov
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
Wei Ru
 
Angular directive filter and routing
Angular directive filter and routingAngular directive filter and routing
Angular directive filter and routing
jagriti srivastava
 
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
Dan Wahlin
 
AngularJS Framework
AngularJS FrameworkAngularJS Framework
AngularJS Framework
Barcamp Saigon
 
AngularJs presentation
AngularJs presentation AngularJs presentation
AngularJs presentation
Phan Tuan
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
Nir Kaufman
 
Angular js architecture (v1.4.8)
Angular js architecture (v1.4.8)Angular js architecture (v1.4.8)
Angular js architecture (v1.4.8)
Gabi Costel Lapusneanu
 
Angular JS - Introduction
Angular JS - IntroductionAngular JS - Introduction
Angular JS - Introduction
Sagar Acharya
 
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
Simon Guest
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
Keith Bloomfield
 
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...
FalafelSoftware
 
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
EPAM Systems
 
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
Learnimtactics
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
Jussi Pohjolainen
 

What's hot (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
 

Similar to Filters in AngularJS

Angular Presentation
Angular PresentationAngular Presentation
Angular PresentationAdam Moore
 
AngularJS
AngularJSAngularJS
AngularJS
LearningTech
 
AngularJS Basics
AngularJS BasicsAngularJS Basics
AngularJS Basics
Ravi Mone
 
Angular js
Angular jsAngular js
Angular js
prasaddammalapati
 
Angular workshop - Full Development Guide
Angular workshop - Full Development GuideAngular workshop - Full Development Guide
Angular workshop - Full Development Guide
Nitin Giri
 
Javascripting.pptx
Javascripting.pptxJavascripting.pptx
Javascripting.pptx
Vinod Srivastava
 
Angular.js Primer in Aalto University
Angular.js Primer in Aalto UniversityAngular.js Primer in Aalto University
Angular.js Primer in Aalto University
SC5.io
 
AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014
Dariusz Kalbarczyk
 
Angular js
Angular jsAngular js
Angular js
Baldeep Sohal
 
Angular js
Angular jsAngular js
Angular js
ParmarAnisha
 
Basics of AngularJS
Basics of AngularJSBasics of AngularJS
Basics of AngularJS
Filip Janevski
 
Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete Course
EPAM Systems
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs Workshop
Ran Wahle
 
Angular
AngularAngular
Angular
LearningTech
 
Scal`a`ngular - Scala and Angular
Scal`a`ngular - Scala and AngularScal`a`ngular - Scala and Angular
Scal`a`ngular - Scala and Angular
Knoldus Inc.
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014
Ran Wahle
 
Method and decorator
Method and decoratorMethod and decorator
Method and decorator
Celine George
 
Working with Javascript in Rails
Working with Javascript in RailsWorking with Javascript in Rails
Working with Javascript in Rails
Seungkyun Nam
 

Similar to 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
 

Recently uploaded

TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 

Recently uploaded (20)

TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.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); }; });