Source Address: http://www.ncloud.hk/technology share/angularjs in the filter-filter/
Filters can be used to format data. For example, the time is formatted into a form of YYYY-MM-DD to be presented to the user for easy viewing. Angularjs built in some filters, we can also customize some filters according to our own needs.
Here are the filters provided by Angularjs:
Currency: Format a number as a currency. When no currency symbol is provided, the default symbol for the current region is used.
Number: Formatting numbers becomes text
Filter: Select a subset from the array and return it as a new array
Date: Formatting dates as strings based on the required format
JSON: Allows you to convert a JavaScript object into a JSON string
Lowercase: convert string becomes lowercase
Uppercase: Converting a string to uppercase
LimitTo: Limits the array/string to a specified number of elements/characters.
ORDER BY: Specifies an array by the given expression, which is an alphabetical string and numeric value.
Usage Description:
Usage in HTML:
Filter can be used with pipe characters | Adding to an expression: {{expression | filter}}
You can also use multiple filter with the output of the previous filter as the output of the next filter:
{{expression | filter1 | filter2 | ...}}
Parameters can also be used in directives:
Example: <li ng-repeat= "x in Names | By: ' Country ' >
or <li ng-repeat= "x in Names | Filter: ' I ' >
Usage in javascript:
Example: $scope. formatdate = $filter (' Date ') (today, ' yyyy-mm-dd ');
Sometimes Angularjs built-in filter is not enough, we can define a filter by ourselves.
For example, define a filter named "MyFormat".
<ul ng-app="myApp"ng-controller="Namesctrl">
<li ng-repeat="x in Names">
{{x | myformat}}
</li>
</ul>
<script>
var app = Angular.module (' myApp ', []);
App.filter (' MyFormat ', function() {
returnfunction(x) {
var i, c, txt = "";
x = X.split ("")
for (i = 0; i < x.length; i++) {
c = X[i];
if (i% 2 = = 0) {
c = C.touppercase ();
}
TXT + = c;
}
return txt;
};
});
App.controller (' Namesctrl ', function($scope) {
$scope. Names = [' Jani ', ' Carl ', ' Margareth ', ' Hege ', ' Joe ', ' Gustav ' , ' Birgit ',' Mary ', ' Kai ';
});
</script>
This myformat filter will format the other odd digits with uppercase characters.
Filter (filters) in the Angularjs