AngularJSAngularJS1.X学学笔记7-过滤器

  近日在座笔试被虐成狗了,感觉温馨的算法太弱了。不过仍然先花点事件将那几个AngularJS学习完。明天学习filter

二、创制自定义过滤器

  1、单值过滤

  

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>自定义-过滤单个数据值</title>
</head>
<body ng-controller="tableCtrl">
    <h1>今天是:{{now|date:"dd MMMM yyyy"}}</h1>
    <table width="100%">

        <tr>
            <td>name</td>
            <td>cat</td>
            <td>price</td>
            <td>expiry</td>
            <td>json</td>
        </tr>

        <tr ng-repeat="item in products">
            <td>{{item.name | labelCase:"###"}}</td>
            <td>{{item.cat | uppercase}}</td>
            <td>{{item.price | currency:"¥"}}</td>
            <td>{{item.expiry | number:0}}</td>
            <td>{{item | json}}</td>
        </tr>
    </table>


    <script type="text/javascript" src="../node_modules/angular/angular.min.js"></script>
    <script type="text/javascript">
        angular.module("myApp",[])
        .controller("tableCtrl",function($scope){
            $scope.products = [
                {name:"苹果",cat:"水果",price:5.0,expiry:100000000},
                {name:"香蕉",cat:"水果",price:6.0,expiry:9},
                {name:"桃子",cat:"水果",price:7.0,expiry:7},

                {name:"22",cat:"yy",price:5.0,expiry:5},
                {name:"33",cat:"yy",price:9.0,expiry:6},
                {name:"44",cat:"yy",price:4.0,expiry:2},
                {name:"55",cat:"yy",price:8.0,expiry:4},

                {name:"ww",cat:"tt",price:4.0,expiry:7},
                {name:"qq",cat:"tt",price:3.0,expiry:9},

            ];

                var now = new Date();
                $scope.now = now;

        })
        .filter("labelCase",function(){
            return function(value,pre){
                if(angular.isString(value)){
                    return pre+value;
                }else{
                    return value;
                }
            }
        });
    </script>

</body>
</html>

  我们用filter方法成立自定义过滤器,filter方法有七个参数,第二个指定过滤器的称号,首个是一个工厂函数,在工厂函数中回到一个工友函数,工人函数的首先个参数是现阶段值,其他参数是自定义参数。在本例中定义了一个labelCase过滤器,它的法力是在字符串前边加上自定义的前缀。

  (2)创制一个集结过滤器。

  跟过滤单个值的过滤器大致,不过它要求传入的是一个数组,再次来到的要么一个数组

  

.filter("skip",function(){
            return function(data,num){
                if(angular.isArray(data)&&angular.isNumber(num)){
                    if(num>data.length || num<1){
                        return data;
                    }else{
                        return data.slice(num);
                    }
                }else{
                    return data;
                }
            }
        });

 

  那个过滤器表示的是跳过起来的多少个数据项不突显。

  (3)站在巨人的肩膀上

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>自定义-过滤单个数据值</title>
</head>
<body ng-controller="tableCtrl">
    <h1>今天是:{{now|date:"dd MMMM yyyy"}}</h1>
    <table width="100%">

        <tr>
            <td>name</td>
            <td>cat</td>
            <td>price</td>
            <td>expiry</td>
            <td>json</td>
        </tr>

        <tr ng-repeat="item in products">
            <td>{{item.name | labelCase2:"###":"@@@"}}</td>
            <td>{{item.cat | uppercase}}</td>
            <td>{{item.price | currency:"¥"}}</td>
            <td>{{item.expiry | number:0}}</td>
            <td>{{item | json}}</td>
        </tr>
    </table>


    <script type="text/javascript" src="../node_modules/angular/angular.min.js"></script>
    <script type="text/javascript">
        angular.module("myApp",[])
        .controller("tableCtrl",function($scope){
            $scope.products = [
                {name:"苹果",cat:"水果",price:5.0,expiry:100000000},
                {name:"香蕉",cat:"水果",price:6.0,expiry:9},
                {name:"桃子",cat:"水果",price:7.0,expiry:7},

                {name:"22",cat:"yy",price:5.0,expiry:5},
                {name:"33",cat:"yy",price:9.0,expiry:6},
                {name:"44",cat:"yy",price:4.0,expiry:2},
                {name:"55",cat:"yy",price:8.0,expiry:4},

                {name:"ww",cat:"tt",price:4.0,expiry:7},
                {name:"qq",cat:"tt",price:3.0,expiry:9},

            ];

                var now = new Date();
                $scope.now = now;

        })
        .filter("labelCase",function(){
            return function(value,pre){
                if(angular.isString(value)){
                    return pre+value;
                }else{
                    return value;
                }
            }
        })
        .filter("labelCase2",function($filter){
            return function(value,pre,end){
                return $filter('labelCase')(value,pre)+end;
            }
        });
    </script>

</body>
</html>

  原来写了一个labelCase,以此为基础写了一个labelCase2,
使之能够而且添加前缀和后缀。总之我是巨人(标题是站在巨人的肩膀上)。

  至此,过滤器就学完了。感觉过滤器即使相比简单,不过效果不小,它可以方便的让相同数据模型有两样的表现方式,卓殊灵活。

一、内置过滤器

  (1)过滤单个数据值

  

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>内置过滤器-过滤单个数据值</title>
</head>
<body ng-controller="tableCtrl">
    <h1>今天是:{{now|date:"dd MMMM yyyy"}}</h1>
    <table width="100%">

        <tr>
            <td>name</td>
            <td>cat</td>
            <td>price</td>
            <td>expiry</td>
            <td>json</td>
        </tr>

        <tr ng-repeat="item in products">
            <td>{{item.name}}</td>
            <td>{{item.cat | uppercase}}</td>
            <td>{{item.price | currency:"¥"}}</td>
            <td>{{item.expiry | number:0}}</td>
            <td>{{item | json}}</td>
        </tr>
    </table>


    <script type="text/javascript" src="../node_modules/angular/angular.min.js"></script>
    <script type="text/javascript">
        angular.module("myApp",[])
        .controller("tableCtrl",function($scope){
            $scope.products = [
                {name:"苹果",cat:"水果",price:5.0,expiry:100000000},
                {name:"香蕉",cat:"水果",price:6.0,expiry:9},
                {name:"桃子",cat:"水果",price:7.0,expiry:7},

                {name:"22",cat:"yy",price:5.0,expiry:5},
                {name:"33",cat:"yy",price:9.0,expiry:6},
                {name:"44",cat:"yy",price:4.0,expiry:2},
                {name:"55",cat:"yy",price:8.0,expiry:4},

                {name:"ww",cat:"tt",price:4.0,expiry:7},
                {name:"qq",cat:"tt",price:3.0,expiry:9},

            ];

                var now = new Date();
                $scope.now = now;

        });
    </script>

</body>
</html>

 

 

AngularJS 1

   上例中用到了大面积的放权过滤单个数据值的过滤器,上边一一表明。

  • currency:格式化货币值,可以在背后加:跟参数,指定货币符号,默许为$
  • number:格式化数字,前边跟:加参数,表示保留的小数位数,它会在千分为加上逗号
  • date:参数为日期格式,可以将Date对象可能皮秒数格式化为指定的格式
  • uppercase:字母大写
  • lowercase:字母小写
  • json:将javascript对象转换为json对象

   (2)过滤集合

  • 限制数量   

  表示只显示products的前3个项,参数为负数表示从最终一个往前算。

  •  选取项

  用filter可以过滤出再次来到true的项,给个函数也是可以的,例如下面

<tr ng-repeat="item in products | filter:selectItems">

$scope.selectItems = function(item){
     return item.cat == "tt" || item.cat == "yy";       
}
  • 排序

 

  默认是升序排列,加个-号则降序,当然也得以对多少个谓词举行排序,像那个样子

<tr ng-repeat="item in products | orderBy:['expiry','-price']'">

  注意充足小小的引号。

相关文章