2016-04-14 30 views
0

RIOT GAMES API'u kullanıyorum. API çağrılarım için fabrikaları kullanıyorum. Her şeyden önce, summonerName isteğinde bulunup, bu adın tanıtımı için bu ismi kullanıyorum. SonraFabrika istekleri aracılığıyla bağımsız değişkenler nasıl iletilir

$scope.summonerId = $scope.summoner.id; 

Ve bu $ kapsamına erişmek ama çalışmıyor:

Ben summonerId recive gereken bu hatayı ve tanımsız YMM

Birlikte çalıştık. Ben summonerId argüman geçmesi

'use strict'; 

angular.module('mean.system').controller('SummonerController', ['$scope', '$http','APIAcces', '$stateParams', 
    function($scope, $http, APIAcces, $stateParams) {   

    $scope.summonerName = $stateParams.summonerName; 

    APIAcces.getSummonerByName($scope.summonerName).then(

     function successCallback(response) { 

      $scope.summoner = response.data[$scope.summonerName.toLowerCase()];     
      $scope.summonerId = $scope.summoner.id; 

      console.log($scope.summonerId); //returns id successfuly 
      console.log(response); 
     }, function errorCallback(error) { 

      console.log(error); 
      console.log(response); 
     }, 

//if I do.. APIAcces.getSummonerSummary('21694436').then(// it works! 
     APIAcces.getSummonerSummary($scope.summonerId).then(

      function successCallback(response) { 

       $scope.summoner2 = response.data[$scope.summonerId]; 
       console.log(response); 

      },function errorCallback(error) { 
       console.log(error); 
       console.log(response); 
      } 
     ) //End APIAcces.getSummonerSummary 

); //End APIAcces.getSummonerByName 

    } 
]); 

ve bu fabrika bunu tanımıyor: Aşağıdaki javascript kodu var

https://euw.api.pvp.net/api/lol/euw/v1.3/stats/by-summoner/undefined/summary?season=SEASON2016&api_key=foo-bar-foo-bar-foo-bar 

: (21694436) bu benim summonerID olduğunu. I use this method:

angular.module('mean.system').factory('APIAcces',['$http','API_KEY', 
     function($http,API_KEY){ 
      return { 
       getSummonerByName:function(summonerName){ 
        return  $http.get('https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/'+summonerName+'?api_key='+API_KEY.KEY); 
       }, 
       getSummonerSummary:function(summonerId){ 
        return  $http.get('https://euw.api.pvp.net/api/lol/euw/v1.3/stats/by-summoner/'+summonerId+'/summary?season=SEASON2016&api_key='+API_KEY.KEY); 
       }, 
      } 
     }]).value('API_KEY',{ 
      KEY: 'foo-bar-foo-bar-foo-bar' 
    }); 
Bilmiyorum

, belki de fabrikalarda falan bir emirdir ?

cevap

1

Kodunuzdan, normal bir async geri arama sorunu var. javascript geri çağırma ve uyumsuz mimariyi başka bir yerde okuyarak anlamanız gerekebilir.

APIAcces.getSummonerSummary() 

APIAcces.getSummonerByName() 

getirilirken bitirmedi zaman denilen summonerId tanımsız nedenle, bu sadece bir zaman uyumsuz programlama doğası var olan çünkü

nedenidir.

APIAcces.getSummonerByName($scope.summonerName).then(
    function(response){ 
     var summonerId; //extract the id from response 
     APIAcces.getSummonerSummary(summonerId).then(
      function(response){ 
       //response should contain the summary 
      }, 
      function(error){ 
       //error of getting by id 
      } 
     ); 
    },function(error){ 
    //error of getting by name 
}); 
+0

zaman uyumsuz o ajax gerekli değil – devqon

+0

olma Ajax'ın doğası, sen bile zaman aşımı gibi "zaman uyumsuz" işlevini() – Kossel

+0

Biliyorum sahibi inşa edebilir * Yani bu daha Bu durumda async olan javascript kendisi değil, ama ajax çağrı çok iyi çalıştı – devqon

0

iki sorun vardır:

yüzden bu sorunu gidermek için, çağrı birlikte böyle zinciri var. Fonksiyonlarınızdan içeriğini Kırpma ilk sorunu gösterir:

APIAcces.getSummonerByName($scope.summonerName).then(

    function successCallback(response) { 
     // success 
    }, 
    function errorCallback(error) { 
     // failure 
    }, 
    APIAcces.getSummonerSummary($scope.summonerId).then(
     function successCallback(response) { 
      // inner success 
     }, 
     function errorCallback(error) { 
      // outer success 
     } 
    ) 
); 

Üçüncü parametre nihayet parametresine ama bir işlevde geçmezken olduğunu. Bunu yapmalısınız:

APIAcces.getSummonerByName($scope.summonerName).then(

    function successCallback(response) { 
     // success 
    }, 
    function errorCallback(error) { 
     // failure 
    }, 
    function finallyCallback() { 
     APIAcces.getSummonerSummary($scope.summonerId).then(
      function successCallback(response) { 
       // inner success 
      }, 
      function errorCallback(error) { 
       // inner failure 
      } 
     ) 
    } 
); 

ikinci sorun muhtemelen nihayet yine bloke içinde istiyorum kalmamasıdır. İsteğiniz başarısız olursa, çalışmak için uygun bir kullanıcı kimliğine sahip olmayacaksınız. Bu başarı bloğuna taşınmış olmalıdır:

APIAcces.getSummonerByName($scope.summonerName).then(

    function successCallback(response) { 
     // success 

     APIAcces.getSummonerSummary($scope.summonerId).then(
      function successCallback(response) { 
       // inner success 
      }, 
      function errorCallback(error) { 
       // inner failure 
      } 
     ) 
    }, 
    function errorCallback(error) { 
     // failure 
    } 
); 
İlgili konular