2017-10-24 5 views
0

私は愚かな小さなサッカーアプリを構築しています。最初のページで、私は国のトップ部門の順位と来週の試合を読み込もうとしています。

RESTful Webサービスを使用してデータを取得するため、非同期でのが実行されます。テーブルは上質ですが、備品はありません。

フィクスチャオブジェクトの配列があり、その中に 'matchday'と 'status'プロパティがあります。 'this.getFixtures'関数を見ると、成功コードブロックを見てください。私がしようとしているのは、ある特定の試合日のための備品のみを表示することです。特定の試合日に1試合残っている試合が残っている場合は、その試合を表示するだけです。そうでなければ、次のマッチデーのフィクスチャを表示します。

'status'プロパティの値は、通常、「SCHEDULED」または「FINISHED」です。成功コードブロックでは、私は言っています:

取得されたループ全体の治具。 このフィクスチャが予定されている場合は、このフィクスチャのマッチデイになります。 この場合、ブレークループ。

私はgetメソッドの外でその値を使用しようとしていますが、私は未定義になっています。成功ブロック外でその値にアクセスする方法はありますか?

$ scope.matchDay関数をフィルタとして使用します。これは、ng-repeatを使用してそのマッチングデーにスケジュールされたフィクスチャを表示するのに役立ちます。

とにかく、長い息切れポストして申し訳ありませんが、ここではコードがあります:

HTML:私はこれまでの2つの問題を参照してください

<div class="grid-x"> 
<div class="medium-8 medium-offset-2 cell"> 
    <div id="premier-league-banner"> 
     <div class="banner-shade"> 
      <div class="grid-x"> 
       <div class="medium-5 cell"> 
        <table> 
         <tr ng-repeat="team in premierLeagueTable.standing | limitTo: 6"> 
          <th>{{ $index + 1 }}</th> 
          <td><img class="prem-thumbnail" src="{{ team.crestURI }}" /></td> 
          <th>{{ team.teamName }}</th> 
          <th>{{ team.playedGames }}</th> 
          <th>{{ team.goalDifference }}</th> 
          <th>{{ team.points }}</th> 
         </tr> 
        </table> 
       </div> 
       <div class="medium-2 cell"> 
        <img src="images/prem-logo.png" /> 
       </div> 
       <div class="medium-5 cell"> 
        <table> 
         <tr ng-repeat="fixture in premierLeagueFixtures.fixtures | filter:{matchday: 10}"> 
          <th>{{fixture.homeTeamName}}</th> 
          <td>vs</td> 
          <th>{{fixture.awayTeamName}}</th> 
         </tr> 
        </table> 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 

角度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; 
    }; 
}]); 

答えて

1

。定義されていない値のメモの1つは、サービスが正しく実装されていない可能性があることです。 AFAIKあなたは "function($ 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; 
}]); 

ので、代わりのないサービスを返さない関数、私はそれを変更したい方法です、あなたは私はあなたが意図したと信じて、あなたのサービスが包まれ、戻ってきました。私はまたあなたの意図(内部サービス変数)が関数内のvarスコープでより雄弁に扱われるので、 "self"への参照を削除しました。

サービスが動作してから表示される2番目の問題です。

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

この行は要求データを返しませんが、要求オブジェクトを返します。実際に$ scope.premierLeagueTableが設定される頃には、要求はまだ完了していません。あなたが得ることは、コールバック関数を置くことができるという約束にアクセスすることです。詳細については、角度リソースのドキュメントを参照してください。具体的には、表示されているuser-resourceセクションの3番目の例$ promise https://docs.angularjs.org/api/ngResource/service/ $ resource#user-resource 。

データの戻り値を適用する機能は、その中にあるべきです。$ promise.then(...)コールバック。私は、そこの約束が応答データを受け取るか、コールバックが返ってくるかどうかは完全にはわかりません。あなたはそれを見つけるためにさらに読むか実験をしなければなりません。

関連する問題