Filter (filters) in the Angularjs

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.