Angularjsfilter filter _ AngularJS

Source: Internet
Author: User
This article mainly introduces angularjsfilter filter related information. If you need it, you can refer to the system to learn angularjs. It is found that some ideas of angularjs are similar to the php module smarty, such as data binding, filter. If you are familiar with smarty, learning angularjs is easier. The filter function of angularjs can be divided into two types: built-in filter and custom filter.

Filter is used to format data.

Basic Filter prototype (''is similar to the pipeline mode in Linux ):

{{ expression filter }}

Filters can be chained (that is, multiple filters are used continuously ):

{{ expression filter1 filter2 ... }}

Filter can also specify multiple parameters:

{{ expression filter:argument1:argument2:... }}

1. built-in filters

1, uppercase, lowercase size conversion

{"Lower cap string" | uppercase} // result: lower cap string {"TANK is GOOD" | lowercase} // result: tank is good

| The vertical line here is a pipeline function. If you are familiar with linux, this line is used. | the Pipeline Function of the root linux is basically the same.

2. json format

{Foo: "bar", baz: 23} | json} // result: {"foo": "bar", "baz": 23}

Note: bza is not formatted with double quotation marks. After formatting, it is converted into json data.

3, date Format

{1304375948024 | date }}// result: May 3, 2011 {1304375948024 | date: "MM/dd/yyyy @ h: mma" }}// result: 05/03/2011 @ 06:39:08 AM {1304375948024 | date: "yyyy-MM-dd hh: mm: ss"} // result:

4. number formatting

{1.234567 | number: 1 }}// result: 1.2 {1234567 | number} // result: 1,234,567

5. Format currency

{250 | currency }}// result: $250.00 {250 | currency: "RMB ¥" }}// result: RMB ¥250.00

6. filter search

{[{"Age": 20, "id": 10, "name": "iphone" },{ "age": 12, "id": 11, "name": "sunm xing" },{ "age": 44, "id": 12, "name": "test abc"}] | filter: 'S '} // find the row containing s // result of the preceding example: [{"age": 12, "id": 11, "name ": "sunm xing" },{ "age": 44, "id": 12, "name": "test abc"}] {[{"age": 20, "id": 10, "name": "iphone" },{ "age": 12, "id": 11, "name": "sunm xing "}, {"age": 44, "id": 12, "name": "test abc"}] | filter: {'name ': 'iphone '}}// find the row whose name is iphone // result of the previous example: [{"age": 20, "id": 10, "name ": "iphone"}]

7, limitTo string, screenshot of the image

{"I love tank" | limitTo: 6} // result: I love {"I love tank" | limitTo:-4} // result: tank {[{"age": 20, "id": 10, "name": "iphone" },{ "age": 12, "id": 11, "name": "sunm xing" },{ "age": 44, "id": 12, "name": "test abc"}] | limitTo: 1 }}// result: [{"age": 20, "id": 10, "name": "iphone"}]

8. Order

{[{"Age": 20, "id": 10, "name": "iphone" },{ "age": 12, "id": 11, "name": "sunm xing" },{ "age": 44, "id": 12, "name": "test abc"}] | orderBy: 'id ': true }}// sort the root id in descending order {[{"age": 20, "id": 10, "name": "iphone" },{ "age ": 12, "id": 11, "name": "sunm xing" },{ "age": 44, "id": 12, "name ": "test abc"}] | orderBy: 'id'} // sort by id in ascending order

Ii. Custom filter Function

I found a mvc Framework of angularjs, phonecat, and custom filter, which is also written on this basis. This framework is quite useful.

1. Add a module to filters. js.

angular.module('tanktest', []).filter('tankreplace', function() { return function(input) { return input.replace(/tank/, "=====") }; }); 

2. Load the module in app. js.

var phonecatApp = angular.module('phonecatApp', [ 'ngRoute', 'phonecatControllers', 'facebookControllers', 'tanktest' ]); 

3, called in html

{"TANK is GOOD" | lowercase | tankreplace} // result: ==== is good

Note: | lowercase | the tankreplace pipeline command can have multiple

The above is a small series of angularjs filter related knowledge, I hope to help you, more about angularjs filter knowledge, please stay tuned to the script home website. Thank you!

Related Article

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.