First, let’s introduce the $filter service:
1. $filter is a dedicated service used for data formatting;
2. AngularJS has built-in currency, date, filter, json, limitTo, lowercase, uppercase, number, orderBy These 8 filters;
3. Filters can be nested and separated by pipe symbols "|" (a bit like Linux);
4. Filters can pass parameters;
5. Users can customize filters.
Introducing the built-in filters:
currency: used to format currency, such as automatically adding "$" or "¥" before the value.
date: Format date, it provides rich date formats.
json: Complete json formatting.
number: Such as converting to two decimal places, etc.
orderBy: Sort. Simple use of
filter:
{{ 1304375948024 | date }} {{ 1304375948024 | date:"MM/dd/yyyy h:mma" }} {{ 1304375948024 | date:"yyyy-MM-dd hh:mm:ss" }} {{ 30 | currency }}
Output:
May 3, 2011
05/03/2011 6:39AM
2011-05-03 06:39:08
$30.00
Customized filter:
eg:
var myModule = angular.module('myModule',[]); myModule.filter('myFilter',function(){ return function(item){ return 'Hi,'+item; } });
Use: {{ 'JennyLin' | myFilter }}