查找當前頁的url
$window.location
Filter是用來格式化數據用的。
Filter的基本原型( '|' 類似于Linux中的管道模式):
{{ expression | filter }}
Filter可以被鏈式使用(即連續使用多個filter):
{{ expression | filter1 | filter2 | ... }}
Filter也可以指定多個參數:
{{ expression | filter:argument1:argument2:... }}
內置的過濾器
貨幣 默認是美元符號$,想要變成其他的如人民幣¥
HTML:{{ currency_expression | currency:symbol:fractionSize}}
JS:$filter(“currency”)(amount,symbol,fractionSize);
<!--html代碼-->
{{ 12 | currency}} <!--將12格式化為貨幣,默認單位符號為 '$', 小數默認2位-->
{{ 12.45 | currency:'¥'}} <!--將12.45格式化為貨幣,使用自定義單位符號為 '¥', 小數默認2位-->
{{ 12.45 | currency:'CHY¥':1}} <!--將12.45格式化為貨幣,使用自定義單位符號為 'CHY¥', 小數指定1位, 會執行四舍五入操作 -->
{{ 12.55 | currency:undefined:0}} <!--將12.55格式化為貨幣, 不改變單位符號, 小數部分將四舍五入 -->
date格式化
HTML:{{date_expression | date:format:timezone}}
JS:$filter(“date”)(date,format,timezone);
<!--使用ISO標準日期格式 -->
{{ '2016-12-12T03:56:16.887Z' | date }} //結果:Dec 12, 2016
{{ 2016/12/12 | date:"yyyy-MM-dd hh:mm:ss" }} //結果:2016-12-12 06:39:08
<!--使用13位(單位:毫秒)時間戳 -->
{{ 1432075948123 | date:"MM/dd/yyyy @ h:mma" }} //結果:12/12/2016 @ 6:39AM
<!--指定timezone為UTC -->
{{ 1432075948123 | date:"MM/dd/yyyy @ h:mma":"UTC"}}
number格式化 如果輸入是null或undefined,那么其將被返回。如果輸入的是無窮(正無窮/負無窮),將會返回無窮大的符號“∞”。如果輸入不是一個數字,返回一個空字符串。
HTML:{{number_expression | number:fractionSize}}
JS:$filter(“number”)(number,fractionSize);
{{ 1.234567 | number:1 }} //結果:1.2
{{ 1234567 | number }} //結果:1,234,567
filter過濾數組
HTML:{{filter_expression | filter:expression:comparator}}
JS:$filter(“filter”)(array,expression,comparator);
<!--用法1(參數expression使用String)-->
{{ [{"age": 20,"id": 10,"name": "iphone"},
{"age": 12,"id": 11,"name": "sunm xing"},
{"age": 44,"id": 12,"name": "test abc"}
] | filter:'s'}} //查找含有有s的行
//上例結果:[{"age":12,"id":11,"name":"sunm xing"},{"age":44,"id":12,"name":"test abc"}]
<!--用法2(參數expression使用object)-->
{{ [{"age": 20,"id": 10,"name": "iphone"},
{"age": 12,"id": 11,"name": "sunm xing"},
{"age": 44,"id": 12,"name": "test abc"}
] | filter:{'name':'iphone'} }} //查找name為iphone的行
//上例結果:[{"age":20,"id":10,"name":"iphone"}]
<!--用法3(參數expression使用function)-->
// 先在Controller中定義function: myFilter
$scope.myFilter = function (item) {
return item.age === 20;
};
<div ng-repeat="u in myArr | filter:myFilter ">
<p>Name:{{u.name}}</p>
<p>Age:{{u.age}}</p>
<br />
</div>
<!--用法4(指定comparator為true或false)-->
<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}]">
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>
<!--用法5(指定comparator為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:20}, {name:'Tom Senior', age:50}, {name:'May', age:21}, {name:'Jack', age:20}, {name:'Alice', age:22}]">
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>
limitTo字符串 對象的截取;創建一個只包含指定數目元素的數組或字符串。元素是按指定的值和符號(+或-)從數組、字符串或數字的開頭或結尾獲取的。如果輸入一個數字,則轉換為字符串。
HTML:{{limitTo_expression | limitTo:limit:begin}}
JS:$filter(“limitTo”)(input,limit,begin);
{{ "i love laiyit" | limitTo:6 }} //結果:i love
{{ "i love laiyit" | limitTo:-4 }} //結果:iyit
{{ [{"age": 20,"id": 10,"name": "iphone"},
{"age": 12,"id": 11,"name": "sunm xing"},
{"age": 44,"id": 12,"name": "test abc"}
] | limitTo:1 }} //結果:[{"age":20,"id":10,"name":"iphone"}]
uppercase,lowercase大小轉換
HTML:{{lowercase_expression | lowercase}}
JS:$filter(“lowercase”)(input);
<!--html代碼-->
{{ "lower cap string" | uppercase }} //結果:LOWER CAP STRING
{{ "LAIYIT is BEAUTIFUL" | lowercase }} //結果:laiyit is beautiful
<!--js代碼-->
$scope.filteredText = $filter('uppercase')($scope.originalText);
json格式化
HTML:{{json_expression | json:spacing}}
JS:$filter(“json”)(object,spacing);
{{ {foo: "bar", baz: 23} | json }} //結果:{ "foo": "bar", "baz": 23 }
orderBy對象排序 通過判斷表達式將指定的數組進行排序。它是按字符串的字母順序和數值的數字排序的。
注意:如果你發現數字沒有按預期排序,請確保它們實際上是被保存為數字而不是字符串。
HTML:{{orderBy_expression | orderBy:expression:reverse}}
JS:$filter(“orderBy”)(array,expression,reverse);
<!--deposit前面的'-'表示deposit這列倒敘排序,默認為順序排序 3 參數reverseOrder:true表示結果集倒敘顯示-->
{{ [{"age": 20,"id": 10,"name": "iphone"},
{"age": 12,"id": 11,"name": "sunm xing"},
{"age": 44,"id": 12,"name": "test abc"}
] | orderBy:['id', '-deposit']:true }} //根id降序排
{{ [{"age": 20,"id": 10,"name": "iphone"},
{"age": 12,"id": 11,"name": "sunm xing"},
{"age": 44,"id": 12,"name": "test abc"}
] | orderBy:'id':true }} //根id降序排
{{ [{"age": 20,"id": 10,"name": "iphone"},
{"age": 12,"id": 11,"name": "sunm xing"},
{"age": 44,"id": 12,"name": "test abc"}
] | orderBy:'id' }} //根據id升序排
自定義過濾器
在filter.js中定義一個module
angular.module('laiyittest', []).filter('laiyitreplace', function() {
return function(input) {
return input.replace(/laiyit/, "***")
};
});
在app.js中加載這個module
var phonecatApp = angular.module('phonecatApp', [
'ngRoute',
'phonecatControllers',
'facebookControllers',
'laiyittest'
]);
在html中調用
{{ "laiyit is GOOD" | lowercase | laiyitreplace}} //結果:*** is good