AngularJS的Filter用法詳解

Filter簡介

Filter是用來格式化數據用的。 javascript

Filter的基本原型( '|' 相似於Linux中的管道模式): html

{{ expression | filter }}

Filter能夠被鏈式使用(即連續使用多個filter): java

{{ expression | filter1 | filter2 | ... }}

Filter也能夠指定多個參數: angularjs

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

 

AngularJS內建的Filter

AngularJS內建了一些經常使用的Filter,咱們一一來看一下。 express

currencyFilter(currency): json

用途:格式化貨幣 api

方法原型: 數組

function(amount, currencySymbol, fractionSize)

 用法: app

複製代碼
1 {{ 12 | currency}} <!--將12格式化爲貨幣,默認單位符號爲 '$', 小數默認2位--> 2 3 {{ 12.45 | currency:'¥'}} <!--將12.45格式化爲貨幣,使用自定義單位符號爲 '¥', 小數默認2位--> 4 5 {{ 12.45 | currency:'CHY¥':1}} <!--將12.45格式化爲貨幣,使用自定義單位符號爲 'CHY¥', 小數指定1位, 會執行四捨五入操做 --> 6 7 {{ 12.55 | currency:undefined:0}} <!--將12.55格式化爲貨幣, 不改變單位符號, 小數部分將四捨五入 -->
複製代碼

 

dateFilter(date): ide

用途:格式化日期

方法原型:

function(date, format, timezone)

用法:

複製代碼
<!--使用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):

用途:過濾數組

方法原型:

function(array, expression, comparator)

用法1(參數expression使用String):

複製代碼
1 <div ng-init="myArr = [{name:'Tom', age:20}, {name:'Tom Senior', age:50}, {name:'May', age:21}, {name:'Jack', age:20}, {name:'Alice', age:22}]"> 2 <!-- 參數expression使用String,將全文搜索關鍵字 'a' --> 3 <div ng-repeat="u in myArr | filter:'a' "> 4 <p>Name:{{u.name}}</p> 5 <p>Age:{{u.age}}</p> 6 <br /> 7 </div> 8 </div> 
複製代碼

用法2(參數expression使用function):

複製代碼
 1 // 先在Controller中定義function: myFilter  2 $scope.myFilter = function (item) {  3  return item.age === 20;  4 };  5  6 <div ng-repeat="u in myArr | filter:myFilter ">  7 <p>Name:{{u.name}}</p>  8 <p>Age:{{u.age}}</p>  9 <br /> 10 </div>
複製代碼

用法3(參數expression使用object):

複製代碼
1 <div ng-init="myArr = [{name:'Tom', age:20}, {name:'Tom Senior', age:50}, {name:'May', age:21}, {name:'Jack', age:20}, {name:'Alice', age:22}]"> 2 <div ng-repeat="u in myArr | filter:{age: 21} "> 3 <p>Name:{{u.name}}</p> 4 <p>Age:{{u.age}}</p> 5 <br /> 6 </div> 7 </div>
複製代碼

用法4(指定comparator爲true或false):

複製代碼
 1 <div ng-init="myArr = [{name:'Tom', age:20}, {name:'Tom Senior', age:50}, {name:'May', age:21}, {name:'Jack', age:20}, {name:'Alice', age:22}]">  2 Name:<input ng-model="yourName" />  3 <!-- 指定comparator爲false或者undefined,即爲默認值可不傳,將以大小寫不敏感的方式匹配任意內容 -->  4 <!-- 能夠試試把下面代碼的comparator爲true,true即大小寫及內容均需徹底匹配 -->  5 <div ng-repeat="u in myArr | filter:{name:yourName}:false ">  6 <p>Name:{{u.name}}</p>  7 <p>Age:{{u.age}}</p>  8 <br />  9 </div> 10 </div>
複製代碼

用法5(指定comparator爲function):

複製代碼
 1 // 先在Controller中定義function:myComparator, 此function將能匹配大小寫不敏感的內容,但與comparator爲false的狀況不一樣的是,comparator必須匹配全文  2 $scope.myComparator = function (expected, actual) {  3  return angular.equals(expected.toLowerCase(), actual.toLowerCase());  4 }  5  6 <div ng-init="myArr = [{name:'Tom', age:20}, {name:'Tom Senior', age:50}, {name:'May', age:21}, {name:'Jack', age:20}, {name:'Alice', age:22}]">  7 Name:<input ng-model="yourName" />  8 <div ng-repeat="u in myArr | filter:{name:yourName}:myComparator ">  9 <p>Name:{{u.name}}</p> 10 <p>Age:{{u.age}}</p> 11 <br /> 12 </div> 13 </div>
複製代碼

 

jsonFilter(json):

 方法原型:

function(object, spacing)

用法(將對象格式化成標準的JSON格式):

{{ {name:'Jack', age: 21} | json}}

 

limitToFilter(limitTo):

 方法原型:

function(input, limit)

用法(選取前N個記錄):

複製代碼
1 <div ng-init="myArr = [{name:'Tom', age:20}, {name:'Tom Senior', age:50}, {name:'May', age:21}, {name:'Jack', age:20}, {name:'Alice', age:22}]"> 2 <div ng-repeat="u in myArr | limitTo:2"> 3 <p>Name:{{u.name}} 4 <p>Age:{{u.age}} 5 </div> 6 </div>
複製代碼

 

lowercaseFilter(lowercase)/uppercaseFilter(uppercase):

 方法原型:

function(string)

用法:

China has joined the {{ "wto" | uppercase }}. We all need {{ "MONEY" | lowercase }}.

 

numberFilter(number):

方法原型:

function(number, fractionSize)

用法:

{{ "3456789" | number}} <br /> {{ true | number}} <br /> {{ 12345678 | number:1}}

 

orderByFilter(orderBy):

方法原型:

function(array, sortPredicate, reverseOrder)

用法:

複製代碼
 1 <div ng-init="myArr = [{name:'Tom', age:20, deposit: 300}, {name:'Tom', age:22, deposit: 200}, {name:'Tom Senior', age:50, deposit: 200}, {name:'May', age:21, deposit: 300}, {name:'Jack', age:20, deposit:100}, {name:'Alice', age:22, deposit: 150}]">  2 <!--deposit前面的'-'表示deposit這列倒敘排序,默認爲順序排序  3  參數reverseOrder:true表示結果集倒敘顯示-->  4 <div ng-repeat="u in myArr | orderBy:['name','-deposit']:true">  5 <p>Name:{{u.name}}</p>  6 <p>Deposit:{{u.deposit}}</p>  7 <p>Age:{{u.age}}</p>  8 <br />  9 </div> 10 </div>
複製代碼

 

自定義Filter

和Directive同樣,若是內建的Filter不能知足你的需求,你固然能夠定義一個專屬於你本身的Filter。咱們來作一個本身的Filter:capitalize_as_you_want,該Filter會使你輸入的字符串中的首字母、指定index位置字母以及指定的字母所有大寫。

方法原型:

function (input, capitalize_index, specified_char)

完整的示例代碼:

複製代碼
 1 <!DOCTYPE>  2 <html>  3 <head>  4 <script src="/Scripts/angular.js"></script>  5 <script type="text/javascript">  6  (function () {  7 var app = angular.module('ngCustomFilterTest', []);  8  9  app.filter('capitalize_as_you_want', function () { 10 return function (input, capitalize_index, specified_char) { 11  input = input || ''; 12 var output = ''; 13 14 var customCapIndex = capitalize_index || -1; 15 16 var specifiedChar = specified_char || ''; 17 18 for (var i = 0; i < input.length; i++) { 19 // 首字母確定是大寫的, 指定的index的字母也大寫 20 if (i === 0 || i === customCapIndex) { 21  output += input[i].toUpperCase(); 22  } 23 else { 24 // 指定的字母也大寫 25 if (specified_char != '' && input[i] === specified_char) { 26  output += input[i].toUpperCase(); 27  } 28 else { 29  output += input[i]; 30  } 31  } 32  } 33 34 return output; 35  }; 36  }); 37 38  })(); 39 </script> 40 </head> 41 <body ng-app="ngCustomFilterTest"> 42 <input ng-model="yourinput" type="text"> 43 <br /> 44  Result: {{ yourinput | capitalize_as_you_want:3:'b' }} 45 </body> 46 </html>
複製代碼

 

好了,本篇講了AngularJS中的Filter,看完這篇後,咱們能夠利用好Filter很是方便的使數據能按咱們的要求進行展現,從而使頁面變得更生動。

 

參考資料

AngularJS官方文檔:https://docs.angularjs.org/guide/filter

CodeSchool快速入門視頻:http://campus.codeschool.com/courses/shaping-up-with-angular-js/intro

Fun with AngularJS filter: http://sarahbranon.com/post/69604997957/fun-with-angularjs-filters-part-1-the-filter

相關文章
相關標籤/搜索