In the 9th day of AngularJS article series, we are going to learn 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
- Introduction to AngularJS – Day 8
Filters
In this article we are going to manipulate data using AngularJS. The AngularJS framework has another useful component ‘filters’. The following are the remaining built in filters in AngularJS:
- Date
The date filter is one of the most useful filter of the framework. Generally, a date value comes from the database or any other source in a raw and generic format. We can use this filter by declaring it inside any expression.
In the following example, we have used the filter on a date variable attached to the scope:
- {
- {
- emp.joinDate | date
- }
- }
The following code is before applying the filter on date:
- <!DOCTYPEhtml>
- <htmlng-app="myApp">
-
- <head>
- <title>Filters in AngularJS</title>
- <scriptsrc="Scripts/angular.min.js">
- </script>
- </head>
-
- <body>
- <divng-controller="viewController">
- <h1>Welcome to C# Corner!</h1>
- <h3>List Of Employees</h3>
- <ol>
- <li><b>Name :</b>{{employees[0].name}}
- <br/><b>Join Date :</b>{{employees[0].joinDate}}
- <br/>
- </li>
- <br/>
- <li><b>Name :</b>{{employees[1].name}}
- <br/><b>Join Date :</b>{{employees[1].joinDate}}
- <br/>
- </li>
- <br/>
- <li><b>Name :</b>{{employees[2].name}}
- <br/><b>Join Date :</b>{{employees[2].joinDate}}
- <br/>
- </li>
- <br/>
- <li><b>Name :</b>{{employees[3].name}}
- <br/><b>Join Date :</b>{{employees[3].joinDate}}
- <br/>
- </li>
- </ol>
- </div>
- <scripttype="text/javascript">
-
- </script>
- </body>
-
- </html>
After execution of above code you can see output like the following without applying the date filter on joinDate:
After applying the various ways of date filter, output like the following:
In the above output you can see various ways with default date directly accessed i.e. first one. The following is the complete code of above example:
- <!DOCTYPEhtml>
- <htmlng-app="myApp">
-
- <head>
- <title>Filters in AngularJS</title>
- <scriptsrc="Scripts/angular.min.js">
- </script>
- </head>
-
- <body>
- <divng-controller="viewController">
- <h1>Welcome to C# Corner!</h1>
- <h3>List Of Employees</h3>
- <ol>
- <li><b>Name :</b>{{employees[0].name}}
- <br/>
- <b>Join Date :</b>{{employees[0].joinDate}}</li>
- <br/>
- <li><b>Name :</b>{{employees[1].name}}
- <br/>
- <b>Join Date :</b>{{employees[1].joinDate|date}}</li>
- <br/>
- <li><b>Name :</b>{{employees[2].name}}
- <br/>
- <b>Join Date :</b>{{employees[2].joinDate|date:'medium'}}</li>
- <br/>
- <li><b>Name :</b>{{employees[3].name}}
- <br/>
- <b>Join Date :</b>{{employees[3].joinDate|date:'MMMMdd/MM/yyyyHH:mm:ss'}}</li>
- </ol>
- </div>
- <scripttype="text/javascript">
-
- </script>
- </body>
-
- </html>
- JSON
This json filter is mostly used for debugging purposes. It might be necessary to display the contents of an object in the JSON format. JSON, also known as JavaScript ObjectNotation, is a lightweight data interchange format. It display data on view in jsonn format. Here's how to use this filter:
The following is the example of how to use json filter in expression:
- <!DOCTYPEhtml>
- <htmlng-app="myApp">
-
- <head>
- <title>Filters in AngularJS</title>
- <scriptsrc="Scripts/angular.min.js">
- </script>
- </head>
-
- <body>
- <divng-controller="viewController">
- <h1>Welcome to C# Corner!</h1>
- <h3>List Of Employees</h3>
- <ulng-repeat="emp in employees">
- <li>{{emp|json}}</li>
- </ul>
- </div>
- <scripttype="text/javascript">
-
- </script>
- </body>
-
- </html>
Here's the image showing how we use json filter:
After executing this code here's the output that shows the following ways,
- limitTo
Sometimes, we need to display text, or even a list of elements, and it might be necessary to limit its size. This filter does exactly that and can be applied to a string or an array.
The following code is an example where there is a limit to the expression:
- {
- {
- expression | limitTo: 10
- }
- }
The following code shows you how to use limitTo filter for text or an array:
- <!DOCTYPEhtml>
- <htmlng-app>
-
- <head>
- <title>Filters in AngularJS</title>
- <scriptsrc="Scripts/angular.min.js">
- </script>
- </head>
-
- <body>
- <div>
- <h1>Welcome to C# Corner!</h1>
- <h3>limitTo filter example</h3>
- <h2>{{'Jeetendra'|limitTo:4}}</h2>
- </div>
- </body>
-
- </html>
See the following screen for how we used limitTo filter in the above example:
After executing above code you can see the following:
- number
The number filter is used to format a string as a number. Similar to the currency and date filters, the locale can be applied to present the number using the conventions of each location.
Also, you can use a fraction-size parameter to support the rounding up of the number:
The following code shows you how to use number filter:
- <!DOCTYPEhtml>
- <htmlng-app>
-
- <head>
- <title>Filters in AngularJS</title>
- <scriptsrc="Scripts/angular.min.js">
- </script>
- </head>
-
- <body>
- <div>
- <h1>Welcome to C# Corner!</h1>
- <h3>number filter example</h3>
- <h2>{{250|number:2}}</h2>
- <h2>{{250.3254|number:2}}</h2>
- <h2>{{250.1234|number:3}}</h2>
- </div>
- </body>
-
- </html>
See the following image, how we use number filter for numbers like decimal points:
The following is the output of above code:
Great, we learned what is filter in AngularJS and various built in filters in AngularJS with 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.