In the 8th day of AngularJS article series, we are going to learning next key players/concept of AngularJS, understanding the concept of Filters in AngularJS. Before moving to key players/concepts of AngularJS, please read my previous articles:
- Introduction to AngularJS – Day 1
- Introduction to AngularJS – Day 2
- Introduction to AngularJS – Day 3
- Introduction to AngularJS – Day 4
- Introduction to AngularJS – Day 5
- Introduction to AngularJS – Day 6
- Introduction to AngularJS – Day 7
Filters
In this article we are going to manipulate data using AngularJS. The AngularJS framework has another useful component ‘filters’. Filters return a subset of item. It is a global function which is used in a view with the help of pipe (|) symbol and parameter separated by colon (:).
Filters are used in the view to format the data. For example, we are showing date on view and we want print date in the format ‘Dec 23, 2015’. It’s not necessary to register a filter in scope. Filters can also be parameterized and you can also use filters in controller. Filters are mostly used with the {{expressions}}.
Syntax:
- {
- {
- expression | filter
- }
- }
- Or {
- {
- expression | filter1 | filter2...
- }
- }
The framework already brings a set of ready to use filters or built in filters like. Now let’s see what the built in filters in AngularJS are.
- Filter
Have you tried filtering any list of data? This filter perform this task, search within an array and apply any filter criteria. The following example shows you how to search name in array of names.
- <!DOCTYPEhtml>
- <htmlng-app="Csharp">
-
- <head>
- <title>Filters in AngularJS</title>
- <scriptsrc="Scripts/angular.min.js">
- </script>
- </head>
- <bodyng-controller="Corner">
- <h2>Welcome to C# Corner</h2> Enter name to search :
- <inputtype="text" ng-model="searchName" />
- <h3>Array of Names</h3>
- <ulng-repeat="name in names | filter:searchName">
- <li>{{name}}</li>
- </ul>
- <scripttype="text/javascript">
- var app = angular.module("Csharp", []); app.controller("Corner", function ($scope) { $scope.names = ['Sonali', 'Varsha', 'Sameer', 'Monika', 'Prasad']; });
- </script>
- </body>
-
- </html>
In the above code we created array of names in controller and accessing using the help of $scope object. We enter value in textbox and that searches in array of names using filter and we bind that entered text using ‘ng-model’ and pass that bind data to filter. Here's the image:
Finally our example is ready to run, after running it will show the following output:
When we start entering text in textbox you can see the following,
This filter or search provide the full text search means, for example above list we enter character only ‘p’ and result come with record ‘Prasad’ because ‘p’ is only present in ‘Prasad’. Next we enter character ‘s’. Here's the output:
- orderBy
This is used for sorting an array. With this orderBy filter, we can order any array based on a predicate expression.
- {
- {
- expression | orderBy: name
- }
- }
The above code is syntax of using orderBy filter with an expression. The following is the list of names without orderBy filter . Here's the output:
After applying the ‘orderBy’ for array of names see the following output:
The following is the complete code of above example:
- <!DOCTYPEhtml>
- <htmlng-app="Csharp">
-
- <head>
- <title>Filters in AngularJS</title>
- <scriptsrc="Scripts/angular.min.js">
- </script>
- </head>
- <bodyng-controller="Corner">
- <h2>Welcome to C# Corner</h2>
- <h3>Array of Names</h3>
- <ulng-repeat="name in names | orderBy:name">
- <li>{{name}}</li>
- </ul>
- <scripttype="text/javascript">
- var app = angular.module("Csharp", []); app.controller("Corner", function ($scope) { $scope.names = ['Sonali', 'Varsha', 'Sameer', 'Monika', 'Prasad', 'Amol']; });
- </script>
- </body>
- </html>
See how to use ‘orderBy’ filter for array of names:
- Currency
The currency filter is used to format a number based on a currency. The basic usage of this filter is without any parameter:
This is used to add the currency symbol to a given value when there is no currency symbol, for example:
- {
- {
- 10 | currency
- }
- } {
- {
- 100 | currency: “Rs.”
- }
- }
The following example shows how to use ‘currency’ filter and with a specific locale currency:
- <!DOCTYPEhtml>
- <htmlng-app>
-
- <head>
- <title>Filters in AngularJS</title>
- <scriptsrc="Scripts/angular.min.js">
- </script>
- </head>
-
- <body>
- <h2>Welcome to C# Corner</h2>
- <h3>Currency Filter</h3>
- <!-- specific locale symbol $ -->
- <h2>{{100|currency}}</h2>
- <!-- specific locale symbol India 'Rs.' -->
- <h2>{{100|currency:"Rs."}}</h2>
- </body>
-
- </html>
The following is the output of above code of ‘currency’ filter with default and specific locale currency output:
- Lowercase and uppercase
This filter is used to transform the text to uppercase or lowercase. The following example shows how to use ‘lowercase’ and ‘uppercase’ filters:
- <!DOCTYPEhtml>
- <htmlng-app>
-
- <head>
- <title>Filters in AngularJS</title>
- <scriptsrc="Scripts/angular.min.js">
- </script>
- </head>
-
- <body>
- <h2>Welcome to C# Corner</h2>
- <h3>lowercase and uppercase Filters</h3>
- <!-- lowercase filter -->
- <h3>Jeetendra{{'GUND'|lowercase}}</h3>
- <!-- uppercase filter -->
- <h3>{{'jeetendra'|uppercase}}Gund</h3>
- </body>
-
- </html>
The following is the output of above HTML code:
Great, we learned what are filters in AngularJS and various built in filters in AngularJS with implementing example successfully.
Summary
I hope that beginners as well as students understood the concept of Filters in AngularJS with example. If you have any suggestion regarding this article, then please contact me. Stay tuned for other concepts of AngularJS coming soon.
Thanks for reading. Learn it! Share it.