美文网首页web前端
angular过滤器

angular过滤器

作者: 苹果橘子味 | 来源:发表于2017-05-08 09:34 被阅读0次

    1.angular过滤器,是用于对数据的格式化,或者筛选的函数,可以直接在模板中通过一种语法使用

    {{expression | filter}}

    {{expression | filter1 | filter2}}

    {{expression | filter1:param,...}}

    2.过滤器的种类

    number数字

    {{123456789| number}}运行结果    ---->123,456,789{{123456789|number:3}}---->123,456,789.000

    currency

    {{1234566789| currency}}--->$1,234,566,789.00{{1234566789| currency:'rmb'}}--->rmb1,234,566,789.00

    date

    today:{{today}}

    ---->today:"2017-04-25T11:21:11.125Z"

    medium:{{today|date:'medium'}}

    ---->medium:Apr 25, 2017 7:21:11 PM

    short:{{today|date:'short'}}

    ---->short:4/25/17 7:21 PM

    fullDate:{{today|date:'fullDate'}}

    ---->fullDate:Tuesday, April 25, 2017

    longDate:{{today|date:'longDate'}}

    ---->longDate:April 25, 2017

    mediumDate:{{today|date:'mediumDate'}}

    ---->mediumDate:Apr 25, 2017

    shortDate:{{today|date:'shortDate'}}

    ---->shortDate:4/25/17

    meduiTime:{{today|date:'meduiTime'}}

    ---->meduiTime:21e25uiTi21e

    shortTime:{{today|date:'shortTime'}}

    ---->shortTime:7:21 PM

    years{{today|date: 'y'}}{{today|date: 'yy'}}--->years 2017 17

    month{{today|date: 'M'}}{{today|date: 'MM'}}{{today|date: 'MMM'}}{{today|date: 'MMMM'}}--->month 4 04 Apr April

    day{{today|date: 'd'}}{{today|date: 'dd'}}{{today|date: 'EEE'}}{{today|date: 'EEEE'}}--->day 25 25 Tue Tuesday

    hour{{today|date: 'HH'}}{{today|date: 'H'}}{{today|date: 'hh'}}{{today|date: 'h'}}--->hour 19 19 07 7

    minute{{today|date: 'm'}}{{today|date: 'mm'}}---->minute 40 40

    minute{{today|date: 's'}}{{today|date: 'ss'}}{{today|date: '.sss'}}--->minute 39 39 .817

    {{today|date: 'y-M-dH:m:s'}}--->2017-4-25 19:40:39

    json 主要用于调试作用

    在js中使用过滤器,必须首先注入$filter//过滤varnumber = $filter('number')(3000);console.log(number);varjsonString = $filter('json')($scope.data);console.log(jsonString);

    limitTo

    {{[1,2,3,4,5,6,7,8,9,10] | limitTo:5}}    ---->[1,2,3,4,5] {{[1,2,3,4,5,6,7,8,9,10] | limitTo:-5}}  ---->[6,7,8,9,10]

    lowercase 小写

    uppercase 大写

    filter 过滤(string | function | object)

    varapp = angular.module('myCart',[]);app.factory('messageFactory',function(){return{        city:[{            name:'上海',            py:'shanghai'},{            name:'北京',            py:'beijing'},{            name:'四川',            py:'sichuan'}];    };});app.controller('firstController',['$scope','messageFactory',function($scope,messageFactory){$scope.data = messageFactory;}]);

    {{ data.city | filter : '上'}}

    {{ data.city | filter : 'name'}}

    {{ data.city | filter : {name:'上'} }}

    {{ data.city | filter : {py:'g'} }}

    运行结果:[{"name":"上海","py":"shanghai"}][][{"name":"上海","py":"shanghai"}][{"name":"上海","py":"shanghai"},{"name":"北京","py":"beijing"}]

    自定义方法实现过滤var app = angular.module('myCart',[]);app.factory('messageFactory',function() {    return {        city:[{            name:'上海',            py:'shanghai'},{            name:'北京',            py:'beijing'},{            name:'四川',            py:'sichuan'}]    };});js 代码app.controller('firstController',['$scope','messageFactory',function($scope,messageFactory) {$scope.data = messageFactory;$scope.checkName = function (obj) {    if (obj.py.indexOf('h') == -1)        return false;    return true;}}]);html 代码

    {{ data.city | filter : checkName}}

    运行结果[{"name":"上海","py":"shanghai"},{"name":"四川","py":"sichuan"}]

    orderBy(String | function | object) 排序,默认是正序,如果要 反序则在前面加负号-

    varapp = angular.module('myCart',[]);app.factory('messageFactory',function(){return{        city:[{            name:'上海',            py:'shanghai'},{            name:'北京',            py:'beijing'},{            name:'四川',            py:'sichuan'}];    };});app.controller('firstController',['$scope','messageFactory',function($scope,messageFactory){$scope.data = messageFactory;}]);

    {{ data.city | orderBy : 'py' }}

    {{ data.city | orderBy : '-py'}}

    运行结果:[{"name":"北京","py":"beijing"},{"name":"上海","py":"shanghai"},{"name":"四川","py":"sichuan"}][{"name":"四川","py":"sichuan"},{"name":"上海","py":"shanghai"},{"name":"北京","py":"beijing"}]

    相关文章

      网友评论

        本文标题:angular过滤器

        本文链接:https://www.haomeiwen.com/subject/nrhmtxtx.html