按日期Angularjs过滤JSON数据

时间:2015-02-05 20:37:26

标签: angularjs

我正在尝试过滤以下JSON,以便我只加载即将到来的灯具,换句话说,从当前时间开始的所有灯具。但没有任何东西加载,我有什么不对,可能是日期的格式,不确定。

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

 .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>

2 个答案:

答案 0 :(得分:0)

我相信你的过滤器应该接受一个值然后返回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
      }
   }

答案 1 :(得分:0)

抱歉,我忘了在app.js中声明过滤器:

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

所以上面的代码都很好:)非常感谢。