2015-02-05 81 views
0

我想过滤下面的JSON,以便我只加载上来和即将到来的灯具,换句话说,从当前时间开始的所有灯具。但没有加载,我有什么不对,也许是日期的格式,不知道。按日期过滤JSON数据Angularjs

JSON http://www.football-data.org/teams/354/fixtures/?callback=JSON_CALLBACK

FILTER

angular.module('PremierLeagueApp', []) 
.filter('upComingFixtures', function() { 
return function (fixtures) { 

    var filtered_list = []; 

    for (var i = 0; i < fixtures.length; i++) { 

    var currentTime = new Date().getTime(); 
    var fixtureDate = new Date(fixtures[i].date).getTime(); 

    if (currentTime <= fixtureDate) { 
     filtered_list.push(fixtures[i]); 
    } 
    } 
    return filtered_list; 
} 
}); 

控制器

.controller('fixturesController', function($scope, $routeParams, footballdataAPIservice) { 
    $scope.id = $routeParams.id; 
    $scope.fixtures = []; 

    footballdataAPIservice.getFixtures($scope.id).success(function (response) { 
     $scope.fixtures = response; 
    }); 


}); 

HTML

<tr ng-repeat="fixture in fixtures.fixtures | upComingFixtures"> 
     <td>{{$index + 1}}</td> 
     <td>{{teamName(fixture.awayTeam)}}</td> 
     </tr> 

回答

0

我相信你的FIL ter应接受一个值,然后返回true/false以包含或不包含该值。事情是这样的

<tr ng-repeat="fixture in fixtures.fixtures | upComingFixtures(fixture)"> 

    $scope.upComingFixtures = function(fixture) { 

     if(fixture time > current time) { 
     return true; // include this 
     } else { 
     return false; // Don't include this 
     } 
    } 
0

对不起,我忘了申报过滤器在app.js:

angular.module('PremierLeagueApp', [ 
    'PremierLeagueApp.services', 
    'PremierLeagueApp.controllers', 
    'PremierLeagueApp.filters', 
    'ngRoute','ngAnimate' 
]). 

因此,所有的罚款,在任何情况下:)谢谢上面的代码。