<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>
时间: 2024-12-15 05:05:30