系統(tǒng)的學(xué)習(xí)了一下angularjs,發(fā)現(xiàn)angularjs的有些思想根php的模塊smarty很像,例如數(shù)據(jù)綁定,filter。如果對smarty比較熟悉的話,學(xué)習(xí)angularjs會比較容易一點,這篇文章給大家介紹angularjs filter用法詳解,感興趣的朋友一起學(xué)習(xí)吧
Filter簡介
Filter是用來格式化數(shù)據(jù)用的。
Filter的基本原型( ‘|' 類似于Linux中的管道模式):
Filter可以被鏈式使用(即連續(xù)使用多個filter):
Filter也可以指定多個參數(shù):
AngularJS內(nèi)建的Filter
AngularJS內(nèi)建了一些常用的Filter,我們一一來看一下。
currencyFilter(currency):
用途:格式化貨幣
方法原型:
用法:
 {{  | currency}}  <!--將格式化為貨幣,默認單位符號為 '$', 小數(shù)默認位-->
 {{ . | currency:'¥'}} <!--將.格式化為貨幣,使用自定義單位符號為 '¥', 小數(shù)默認位-->
 {{ . | currency:'CHY¥':}} <!--將.格式化為貨幣,使用自定義單位符號為 'CHY¥', 小數(shù)指定位, 會執(zhí)行四舍五入操作 -->
 {{ . | currency:undefined:0}} <!--將12.55格式化為貨幣, 不改變單位符號, 小數(shù)部分將四舍五入 -->
dateFilter(date):
用途:格式化日期
方法原型:
用法:
<!--使用ISO標準日期格式 -->
{{ '2015-05-20T03:56:16.887Z' | date:"MM/dd/yyyy @ h:mma"}}
<!--使用13位(單位:毫秒)時間戳 -->
{{ 1432075948123 | date:"MM/dd/yyyy @ h:mma"}}
<!--指定timezone為UTC -->
{{ 1432075948123 | date:"MM/dd/yyyy @ h:mma":"UTC"}}
filterFilter(filter):
用途:過濾數(shù)組
方法原型:
用法1(參數(shù)expression使用String):
 <div ng-init="myArr = [{name:'Tom', age:}, {name:'Tom Senior', age:}, {name:'May', age:}, {name:'Jack', age:}, {name:'Alice', age:}]">   <!-- 參數(shù)expression使用String,將全文搜索關(guān)鍵字 'a' -->   <div ng-repeat="u in myArr | filter:'a' ">     <p>Name:{{u.name}}</p>     <p>Age:{{u.age}}</p>     <br />   </div> </div>用法2(參數(shù)expression使用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>用法3(參數(shù)expression使用object):
 <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>用法4(指定comparator為true或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,即為默認值可不傳,將以大小寫不敏感的方式匹配任意內(nèi)容 -->   <!-- 可以試試把下面代碼的comparator為true,true即大小寫及內(nèi)容均需完全匹配 -->   <div ng-repeat="u in myArr | filter:{name:yourName}:false ">     <p>Name:{{u.name}}</p>     <p>Age:{{u.age}}</p>     <br />   </div> </div>用法5(指定comparator為function):
// 先在Controller中定義function:myComparator, 此function將能匹配大小寫不敏感的內(nèi)容,但與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):
方法原型:
用法(將對象格式化成標準的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這列倒敘排序,默認為順序排序   參數(shù)reverseOrder:true表示結(jié)果集倒敘顯示-->   <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一樣,如果內(nèi)建的Filter不能滿足你的需求,你當(dāng)然可以定義一個專屬于你自己的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非常方便的使數(shù)據(jù)能按我們的要求進行展示,從而使頁面變得更生動。
新聞熱點
疑難解答