This blog discusses Angularjs filter and demonstrates how to use it.
Getting Started
In Directives
Filter syntax in JavaScript veries depending upen the filter components.
Filters AngulrJS provides following built-in filters,
This example displays employee data in a table. Each data of employee is formatted using AngularJs filters. It uses most used filters like date, number and currency. In currency filter we have used custom currency symbol that "Rs"(Indian currency symbol).
Filters are used in Angularjs to search, filter and format data. They can be used in view templates, controllers or services. Angular comes with a collection of built-in filters. In view template(HTML) you can use filters with Angularjs expressions and directives and it customizes with your own as well. To apply filters use pipe character(|), the general syntax in templates is as follows,
In Expression- {{ expression [| filter_name[:parameter_value] ... ] }}
- <li ng-repeat="x in names | [| filter_name[:parameter_value] ... ] ">
Filters AngulrJS provides following built-in filters,
- Date
Format a date to string based on the requested format. For date several formats are available, most used formats are listed below.- yyyy:-4 digits year example-1987
- yy:-2 ditits year example-87
- MMMM:-January-December
- MMMJan-Dec
- MM01-12
- M1-12(there is no zero leading)
- dd01-31
- d1-31(there is no zero leading)
- number
This filter formats number to string, it can be used both in html and javascript. - currency
Filters a number to string as currency, you can define your own custom currency. When no currency symbol is provided, default symbol for current locale is used. - lowercase
Converts string to lowercase. - uppercase
Converts string to uppercase. - filter
Selects a subset of items from array and returns it as a new array. In my next article we will discuss in details. - limitTo
Creates a new array or string containing only a specified number of elements. Will discuss in next article - orderBy
Returns an array containing the items from the specified collection, ordered by a comparator function based on the values computed using the expression predicate. We will discuss in details in my upcoming article - json
Allows you to convert a JavaScript object into JSON string.
This example displays employee data in a table. Each data of employee is formatted using AngularJs filters. It uses most used filters like date, number and currency. In currency filter we have used custom currency symbol that "Rs"(Indian currency symbol).
Join the conversation! Your thoughts help the community grow.