angular 的 filter用法

查找当前页的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);


{{ 12 | currency}} 
{{ 12.45 | currency:'¥'}}  
{{ 12.45 | currency:'CHY¥':1}} 
{{ 12.55 | currency:undefined:0}} 

date格式化

HTML:{{date_expression | date:format:timezone}}
JS:$filter(“date”)(date,format,timezone);


{{ '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 

{{ 1432075948123 | date:"MM/dd/yyyy @ h:mma" }}                  //结果:12/12/2016 @ 6:39AM  

{{ 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);


{{ [{"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"}]  
  
{{ [{"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"}] 

// 先在Controller中定义function: myFilter 
$scope.myFilter = function (item) { 
  return item.age === 20; 
}; 

Name:{{u.name}}

Age:{{u.age}}


Name:

Name:{{u.name}}

Age:{{u.age}}


// 先在Controller中定义function:myComparator, 此function将能匹配大小写不敏感的内容,但与comparator为false的情况不同的是,comparator必须匹配全文 $scope.myComparator = function (expected, actual) { return angular.equals(expected.toLowerCase(), actual.toLowerCase()); }
Name:

Name:{{u.name}}

Age:{{u.age}}


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);


{{ "lower cap string" | uppercase }}     //结果:LOWER CAP STRING  
{{ "LAIYIT is BEAUTIFUL" | lowercase }}         //结果:laiyit is beautiful 

$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);



{{ [{"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

参考资料:
推酷:angular中的filter用法详解
野兽:angularjs filter过滤器

你可能感兴趣的:(angular 的 filter用法)