Home > Article > Web Front-end > A brief analysis of AngularJS Filter usage_AngularJS
I studied angularjs systematically and found that some ideas of angularjs are very similar to the PHP module smarty, such as data binding and filter. If you are familiar with smarty, it will be easier to learn angularjs. This article will introduce you to the detailed usage of angularjs filter. Friends who are interested can learn together
Filter Introduction
Filter is used to format data.
Basic prototype of Filter (‘|’ is similar to pipe mode in Linux):
Filter can be used in a chain (that is, multiple filters are used continuously):
Filter can also specify multiple parameters:
AngularJS built-in Filter
AngularJS has built-in some commonly used Filters, let’s take a look at them one by one.
currencyFilter(currency):
Purpose: Format currency
Method prototype:
Usage:
{{ | currency}} f91b47ec64007ef9202fe68b35957e97
{{ . | currency:'¥'}} cb7bcac964e954e4482371ba68ff4166
{{ . | currency:'CHY¥':}} 9b178e00c231cf4b1bff82a8bd52e6cf
{{ . | currency:undefined:0}} e8cbf022a364c602f4fc64849f7e1bfa
dateFilter(date):
Purpose: format date
Method prototype:
Usage:
0c05052a49f5bb771d55f91676bf488b
{{ '2015-05-20T03:56:16.887Z' | date:"MM/dd/yyyy @ h:mma"}}
5701e9a3da561e3e77af5a113983be67
{{ 1432075948123 | date:"MM/dd/yyyy @ h:mma"}}
3850ffc8c060aa02c04a9765a23e1d21
{{ 1432075948123 | date:"MM/dd/yyyy @ h:mma":"UTC"}}
filterFilter(filter):
Purpose: filter array
Method prototype:
Usage 1 (use String for parameter expression):
<div ng-init="myArr = [{name:'Tom', age:}, {name:'Tom Senior', age:}, {name:'May', age:}, {name:'Jack', age:}, {name:'Alice', age:}]"> <!-- 参数expression使用String,将全文搜索关键字 'a' --> <div ng-repeat="u in myArr | filter:'a' "> <p>Name:{{u.name}}</p> <p>Age:{{u.age}}</p> <br /> </div> </div>
Usage 2 (parameter expression uses function):
// 先在Controller中定义function: myFilter $scope.myFilter = function (item) { return item.age === ; }; <div ng-repeat="u in myArr | filter:myFilter "> <p>Name:{{u.name}}</p> <p>Age:{{u.age}}</p> <br /> </div>
Usage 3 (use object for parameter expression):
<div ng-init="myArr = [{name:'Tom', age:}, {name:'Tom Senior', age:}, {name:'May', age:}, {name:'Jack', age:}, {name:'Alice', age:}]"> <div ng-repeat="u in myArr | filter:{age: } "> <p>Name:{{u.name}}</p> <p>Age:{{u.age}}</p> <br /> </div> </div>
Usage 4 (specify comparator as true or false):
<div ng-init="myArr = [{name:'Tom', age:}, {name:'Tom Senior', age:}, {name:'May', age:}, {name:'Jack', age:}, {name:'Alice', age:}]"> Name:<input ng-model="yourName" /> <!-- 指定comparator为false或者undefined,即为默认值可不传,将以大小写不敏感的方式匹配任意内容 --> <!-- 可以试试把下面代码的comparator为true,true即大小写及内容均需完全匹配 --> <div ng-repeat="u in myArr | filter:{name:yourName}:false "> <p>Name:{{u.name}}</p> <p>Age:{{u.age}}</p> <br /> </div> </div>
Usage 5 (specify comparator as function):
// 先在Controller中定义function:myComparator, 此function将能匹配大小写不敏感的内容,但与comparator为false的情况不同的是,comparator必须匹配全文 $scope.myComparator = function (expected, actual) { return angular.equals(expected.toLowerCase(), actual.toLowerCase()); } <div ng-init="myArr = [{name:'Tom', age:}, {name:'Tom Senior', age:}, {name:'May', age:}, {name:'Jack', age:}, {name:'Alice', age:}]"> Name:<input ng-model="yourName" /> <div ng-repeat="u in myArr | filter:{name:yourName}:myComparator "> <p>Name:{{u.name}}</p> <p>Age:{{u.age}}</p> <br /> </div> </div>
jsonFilter(json):
Method prototype:
用法(将对象格式化成标准的JSON格式):
limitToFilter(limitTo):
方法原型:
用法(选取前N个记录):
<div ng-init="myArr = [{name:'Tom', age:}, {name:'Tom Senior', age:}, {name:'May', age:}, {name:'Jack', age:}, {name:'Alice', age:}]"> <div ng-repeat="u in myArr | limitTo:"> <p>Name:{{u.name}} <p>Age:{{u.age}} </div> </div>
lowercaseFilter(lowercase)/uppercaseFilter(uppercase):
方法原型:
用法:
China has joined the {{ "wto" | uppercase }}. We all need {{ "MONEY" | lowercase }}.
numberFilter(number):
方法原型:
用法:
{{ "3456789" | number}} <br /> {{ true | number}} <br /> {{ 12345678 | number:1}}
orderByFilter(orderBy):
方法原型:
用法:
<div ng-init="myArr = [{name:'Tom', age:, deposit: }, {name:'Tom', age:, deposit: }, {name:'Tom Senior', age:, deposit: }, {name:'May', age:, deposit: }, {name:'Jack', age:, deposit:}, {name:'Alice', age:, deposit: }]"> <!--deposit前面的'-'表示deposit这列倒叙排序,默认为顺序排序 参数reverseOrder:true表示结果集倒叙显示--> <div ng-repeat="u in myArr | orderBy:['name','-deposit']:true"> <p>Name:{{u.name}}</p> <p>Deposit:{{u.deposit}}</p> <p>Age:{{u.age}}</p> <br /> </div> </div>
自定义Filter
和Directive一样,如果内建的Filter不能满足你的需求,你当然可以定义一个专属于你自己的Filter。我们来做一个自己的Filter:capitalize_as_you_want,该Filter会使你输入的字符串中的首字母、指定index位置字母以及指定的字母全部大写。
方法原型:
完整的示例代码:
<!DOCTYPE> <html> <head> <script src="/Scripts/angular.js"></script> <script type="text/javascript"> (function () { var app = angular.module('ngCustomFilterTest', []); app.filter('capitalize_as_you_want', function () { return function (input, capitalize_index, specified_char) { input = input || ''; var output = ''; var customCapIndex = capitalize_index || -; var specifiedChar = specified_char || ''; for (var i = ; i < input.length; i++) { // 首字母肯定是大写的, 指定的index的字母也大写 if (i === || i === customCapIndex) { output += input[i].toUpperCase(); } else { // 指定的字母也大写 if (specified_char != '' && input[i] === specified_char) { output += input[i].toUpperCase(); } else { output += input[i]; } } } return output; }; }); })(); </script> </head> <body ng-app="ngCustomFilterTest"> <input ng-model="yourinput" type="text"> <br /> Result: {{ yourinput | capitalize_as_you_want::'b' }} </body> </html>
好了,本篇讲了AngularJS中的Filter,看完这篇后,我们可以利用好Filter非常方便的使数据能按我们的要求进行展示,从而使页面变得更生动。