Filters in AngularJS
What are filters in AngularJS?
Filter is used to format the value of an expression to display in desired format. It can be used in View templates, controllers or services. We can also define our own filter in AngularJS.
Syntax :
{{ data | filter:options}}
There are following filters in AngularJS.
- currency – is used to filter numeric data into localized format, if no prefix is applied
- number – format a number as text. Number is rounded to decimal places and places “,” after each third digit
- uppercase – is used to convert the data into upper case
- lowercase – is used to convert the data into lower case
- date: ‘MM/dd/yyyy h:mma’ – used to convert the timestamp into the format we want
- limitTo:10 – used to write number of characters from the string or number of items from an array.
{{‘Sheo Narayan’, limitTo:4 }} will output "Sheo"
<li data-ng-repeat="username in usernames | limitTo: 2">{{username}}</li> - will output 2 characters of the username
- orderBy – allows us to sort the product by name.
- json – converts a JavaScript object to json.
We shall learn more about filters in coming posts.
<div ng-app>
<p>All upper: {{'sheonarayan' | uppercase}}</p>
<p>Limit to 4 : {{'Sheo Narayan' | limitTo:4 }}</p>
<p>All lower: {{'SHEONARAYAN' | lowercase }}</p>
<p>Currency: {{'2552.25' | currency }}</p>
<p>Number: {{'2552' | number }}</p>
</div>
Views: 11786 | Post Order: 10