码迷,mamicode.com
首页 > 其他好文 > 详细

angular自定义过滤器

时间:2015-04-03 01:38:27      阅读:422      评论:0      收藏:0      [点我收藏+]

标签:angular   angular过滤器   

<html ng-app="Demo">
<head>
    <meta charset="utf-8">
    <title>Order</title>
</head>
<body>
<script src="js/angular.min.js"></script>
<script>
        var app = angular.module('Demo', []);

        app.controller('TestCtrl', function($scope) {

            $scope.name = 'jack';
        	$scope.country = 'American';

            $scope.persons = [
                {name:'qiu',country:'china'},
                {name:'jack',country:'American'},
                {name:'sanlang',country:'Japan'}
            ]

            $scope.setParam = function(name,value){
        		$scope.name = name;
        		$scope.country = value;
        	}
        })

        app.filter('myfilter',function() {
            return function(input, name, country) {
                var output = [];
                var outputother = [];
                var outputtotal = [];
                for (var i = 0; i <= input.length - 1; i++) {
                	console.log(input[i].name);
                	if(input[i].name == name && input[i].country == country){
						output.push(input[i]);
                	}else{
                		outputother.push(input[i]);
                	}
                };
                for (var i = 0; i <= input.length-1; i++) {
                	if(output.length > i){
                		outputtotal.push(output[i]);
                	}else{
                		outputtotal.push(outputother[i - output.length])
                	}
                };
                return outputtotal;
            }
        })
   </script>


<div ng-controller="TestCtrl">
<p>姓名的排序:</p>
<ul>
  <li ng-repeat="person in persons | myfilter:'jack':'American'">
    {{ person.name + ', ' + person.country }}
  </li>
</ul>
</div>
</body>
</html>
	

angular自定义过滤器

标签:angular   angular过滤器   

原文地址:http://blog.csdn.net/long511703/article/details/44841477

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!