Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Angular-尝试访问$http之外的值会获得成功,并将其用作筛选器值_Angularjs_Angular Http_Ngresource - Fatal编程技术网

Angularjs Angular-尝试访问$http之外的值会获得成功,并将其用作筛选器值

Angularjs Angular-尝试访问$http之外的值会获得成功,并将其用作筛选器值,angularjs,angular-http,ngresource,Angularjs,Angular Http,Ngresource,我正在构建一个愚蠢的小足球应用程序。在第一页,我试图载入国家顶级联赛排名和下周的赛程 我使用RESTful Web服务检索数据,因此是异步完成的。桌子很好,但没有固定装置 有一个fixture对象数组,其中有一个“matchday”和“status”属性。如果查看“this.getFixtures”函数,请查看成功代码块。我想做的只是展示某一比赛日的赛具。如果在某个比赛日还有一场比赛要打,那么我只想展示那场比赛。如果没有,则显示下一个比赛日的赛程 “status”属性的值通常为“SCHEDULE

我正在构建一个愚蠢的小足球应用程序。在第一页,我试图载入国家顶级联赛排名和下周的赛程

我使用RESTful Web服务检索数据,因此是异步完成的。桌子很好,但没有固定装置

有一个fixture对象数组,其中有一个“matchday”和“status”属性。如果查看“this.getFixtures”函数,请查看成功代码块。我想做的只是展示某一比赛日的赛具。如果在某个比赛日还有一场比赛要打,那么我只想展示那场比赛。如果没有,则显示下一个比赛日的赛程

“status”属性的值通常为“SCHEDULED”或“FINISHED”。在成功代码块中,我说:

通过检索到的所有装置进行循环。 如果这场比赛已经安排好了,那就意味着我们就在这场比赛的比赛日。 在这种情况下,断开循环

然后我尝试在get方法之外使用该值,但我一直没有定义。是否有任何方法可以在成功块之外访问该值

我将使用$scope.matchDay函数作为过滤器。这将帮助我仅显示该比赛日中具有ng repeat的预定装置

无论如何,很抱歉这篇冗长的文章,但代码如下:

HTML:

角JS

    // MODULE
var quickEleven = angular.module('quickEleven', ['ngRoute', 'ngResource']);

// ROUTES
quickEleven.config(function ($routeProvider) {

    $routeProvider

    .when('/', {
        templateUrl: 'pages/home.htm',
        controller: 'homeController'
    })

});

// CONTROLLERS
quickEleven.controller('homeController', ['$scope', '$resource', '$log', 'footballData', function($scope, $resource, $log, footballData) {

    function getMonday(date) {
        var day = date.getDay() || 7;  
    if( day !== 1 ) 
        date.setHours(-24 * (day - 1)); 
        return date;
    }

    function convertDate(date) {
      var yyyy = date.getFullYear().toString();
      var mm = (date.getMonth()+1).toString();
      var dd  = date.getDate().toString();

      var mmChars = mm.split('');
      var ddChars = dd.split('');

      return yyyy + '-' + (mmChars[1]?mm:"0"+mmChars[0]) + '-' + (ddChars[1]?dd:"0"+ddChars[0]);
    }



    var thisMonday = getMonday(new Date);
    var nextMonday = getMonday(new Date);
    nextMonday.setDate(nextMonday.getDate() + 7);

    $log.info("Boom! " + convertDate(thisMonday));
    $log.info("For! " + convertDate(nextMonday));

    $scope.premierLeagueTable = footballData.getLeagueTable("http://api.football-data.org/v1/competitions/:competitionId/leagueTable", 445);
    //http://api.football-data.org/v1/competitions/:competitionId/fixtures?timeFrameStart=2018-03-01&timeFrameEnd=2018-03-05
    //"http://api.football-data.org/v1/competitions/:competitionId/fixtures/?matchday=9"
    $scope.premierLeagueFixtures = footballData.getFixtures("http://api.football-data.org/v1/competitions/:competitionId/fixtures?timeFrameStart=" + convertDate(thisMonday) + "&timeFrameEnd=" + convertDate(nextMonday), 445);
     $log.info($scope.premierLeagueFixtures);
     $log.info($scope.premierLeagueTable);

    $scope.matchdayValue = 9;

    $scope.matchDay = function() {
        return footballData.getMatchday();
    };

}]);


quickEleven.service('footballData', ['$resource', '$log', function($resource, $log) {

    //Referring to the latest matchday with the status as 'SCHEDULED'
    var self = this;
    var test;
    self.latestScheduledMatchday = 0;

    self.getMatchday = function() {
        $log.info("This is: " + test);
        return self.latestScheduledMatchday;
    }

    this.getLeagueTable = function (footballUrl, compId) {
        this.footballAPI = 
            $resource(footballUrl, {}, {    
                get: {
                    method: "GET",
                        headers: {
                            "X-Auth-Token": "f73808b698e84dccbe4886da3ea6e755"
                        }
                }
            })

            .get({
                competitionId: compId
            }, function(data) {
                this.fussball = data; 
            }, function(err) {
                $log.error(err);
            });

        return this.footballAPI;
    };



    this.getFixtures = function (footballUrl, compId) {
  //      var self;
        this.footballAPI = 
            $resource(footballUrl, {}, {    
                get: {
                    method: "GET",
                        headers: {
                            "X-Auth-Token": "f73808b698e84dccbe4886da3ea6e755"
                        }
                }
            })

            .get({
                competitionId: compId
            }, function(data) {
//                self = data.fixtures;
                self.latestScheduledMatchday = data.fixtures[0].matchday
                for (var i = 0; i < data.fixtures.length; i++) {
                    var fixture = data.fixtures[i];

                    if (fixture.status == 'SCHEDULED') {
                        test = fixture.matchday;
                        break;
                    } 
                }
                $log.info("Dollar signs... " + test);
            }, function(err) {
                $log.error(err);
            });

        return this.footballAPI;
    };
}]);

到目前为止,我看到了两个问题。需要注意的一点是,未定义的值可能无法正确实现您的服务。AFAIK您应该在函数$resource、$log{函数中返回服务

这是我如何改变的注意我没有测试过这个

quickEleven.service('footballData', ['$resource', '$log', function($resource, $log) {

  //Referring to the latest matchday with the status as 'SCHEDULED'
  var wrappedService = {};
  var test;
  var latestScheduledMatchday = 0;

  var getMatchday = function() {
      $log.info("This is: " + test);
      return latestScheduledMatchday;
  }

  wrappedService.getLeagueTable = function (footballUrl, compId) {
      wrappedService.footballAPI = 
          $resource(footballUrl, {}, {    
              get: {
                  method: "GET",
                      headers: {
                          "X-Auth-Token": "f73808b698e84dccbe4886da3ea6e755"
                      }
              }
          })

          .get({
              competitionId: compId
          }, function(data) {
              wrappedService.fussball = data; 
          }, function(err) {
              $log.error(err);
          });

      return wrappedService.footballAPI;
  };



  wrappedService.getFixtures = function (footballUrl, compId) {
      wrappedService.footballAPI = 
          $resource(footballUrl, {}, {    
              get: {
                  method: "GET",
                      headers: {
                          "X-Auth-Token": "f73808b698e84dccbe4886da3ea6e755"
                      }
              }
          })

          .get({
              competitionId: compId
          }, function(data) {
              latestScheduledMatchday = data.fixtures[0].matchday
              for (var i = 0; i < data.fixtures.length; i++) {
                  var fixture = data.fixtures[i];

                  if (fixture.status == 'SCHEDULED') {
                      test = fixture.matchday;
                      break;
                  } 
              }
              $log.info("Dollar signs... " + test);
          }, function(err) {
              $log.error(err);
          });

      return wrappedService.footballAPI;
  };
  return wrappedService;
}]);
此行不返回请求数据,但返回请求对象。事实上,在设置$scope.premierLeagueTable时,请求甚至还没有完成。您得到的是一个承诺,您可以将回调函数放入其中。有关更多信息,请参阅angular resource文档,特别是用户手册中的第三个示例-您看到的资源部分。$PROMITE$resourceuser资源

无论您希望将数据返回应用到哪个功能,都应该在其中。$PROMITE。然后…回调。我不完全确定其中的PROMITE是否接收到响应数据,或者您的回调返回。您必须进一步阅读或实验才能找到答案

quickEleven.service('footballData', ['$resource', '$log', function($resource, $log) {

  //Referring to the latest matchday with the status as 'SCHEDULED'
  var wrappedService = {};
  var test;
  var latestScheduledMatchday = 0;

  var getMatchday = function() {
      $log.info("This is: " + test);
      return latestScheduledMatchday;
  }

  wrappedService.getLeagueTable = function (footballUrl, compId) {
      wrappedService.footballAPI = 
          $resource(footballUrl, {}, {    
              get: {
                  method: "GET",
                      headers: {
                          "X-Auth-Token": "f73808b698e84dccbe4886da3ea6e755"
                      }
              }
          })

          .get({
              competitionId: compId
          }, function(data) {
              wrappedService.fussball = data; 
          }, function(err) {
              $log.error(err);
          });

      return wrappedService.footballAPI;
  };



  wrappedService.getFixtures = function (footballUrl, compId) {
      wrappedService.footballAPI = 
          $resource(footballUrl, {}, {    
              get: {
                  method: "GET",
                      headers: {
                          "X-Auth-Token": "f73808b698e84dccbe4886da3ea6e755"
                      }
              }
          })

          .get({
              competitionId: compId
          }, function(data) {
              latestScheduledMatchday = data.fixtures[0].matchday
              for (var i = 0; i < data.fixtures.length; i++) {
                  var fixture = data.fixtures[i];

                  if (fixture.status == 'SCHEDULED') {
                      test = fixture.matchday;
                      break;
                  } 
              }
              $log.info("Dollar signs... " + test);
          }, function(err) {
              $log.error(err);
          });

      return wrappedService.footballAPI;
  };
  return wrappedService;
}]);
    $scope.premierLeagueTable = footballData.getLeagueTable("http://api.football-data.org/v1/competitions/:competitionId/leagueTable", 445);