0

i work with Riot game API , a create a factory and use where i need get and sort data

        angular.module('starter.services', [])

    .factory('API',function($http){
    var data={};
    var token = "****";
    return{
        Getalldata : function(name){


            $http.get("https://eune.api.pvp.net/api/lol/eune/v1.4/summoner/by-name/" + name, {
          params: {
            api_key: token
          }
        })
        .success(function(response, error) {

          var dbc = [];
          //console.log(response);
          res = response[name];
          //console.log(res);
          id = res.id;


          //$scope.img = "http://sk2.op.gg/images/profile_icons/profileIcon"+res.profileIconId+".jpg";
          $http.get("https://eune.api.pvp.net/api/lol/eune/v1.3/stats/by-summoner/" + res.id + "/summary", {
              params: {
                season: "SEASON2015",
                api_key: token
              }
            })
            .success(function(response, error) {
              //$scope.stat=response.playerStatSummaries
              response.playerStatSummaries.forEach(function(entry) {
                  //console.log(entry);
                  if(entry.playerStatSummaryType=="Unranked"){

                    data.norank5x5=entry;
                  }
                  if(entry.playerStatSummaryType=="CAP5x5"){
                    data.team5x5=entry;
                  }
                  if(entry.playerStatSummaryType=="Unranked3x3"){
                    data.unrank3x3=entry;
                  }
                  if(entry.playerStatSummaryType=="RankedTeam3x3"){
                    data.rank3x3=entry;
                  }
                  if(entry.playerStatSummaryType=="RankedTeam5x5"){
                    data.rank5x5=entry;
                  }

                  //console.log(data.team5x5);

              });
              //console.log(data);
            //return data;

          });

        });

        return date;
        }
        /*getRankData : function(name,sezin){

            mydata = "kola";
            return mydata;

        }*/
    };
});

and use this factory , but in controller in Click i use my factory i get "undefined", how i can get my object ? Controller code :

.controller('MainCtrl', function($scope,$rootScope,$ionicLoading,API) {

  $scope.showmenu = function(){
        console.log(API.Getalldata("fenix"));
    }

});
5
  • can you please provide any errors you are getting? Is your API call working if you hard code in "fenix"? Commented Oct 12, 2015 at 19:38
  • yes , i dont have errors , if i do factory code in controller all fine. Commented Oct 12, 2015 at 19:47
  • see answer below. You are returning "date" not "data" please click check and like if that ends up being your problem. :-) Commented Oct 12, 2015 at 19:49
  • no no , I typed in the editor stackoverflow this error , sorry Commented Oct 12, 2015 at 19:56
  • 1
    show us the onClick ... you don't use that for angular code ... operates in different context Commented Oct 12, 2015 at 19:56

2 Answers 2

3

Return the $http inside the function. Then in the $scope function, do:

API.Getalldata("something").then(function(response) { console.log(response) });

Please note I have not tried this myself, but it should work/help you to get it working.

Btw: returning the date/data is useless as $http is a promise and the variable will not be resolved by the time the value is supposed to be returned.

Sign up to request clarification or add additional context in comments.

5 Comments

I try and get "TypeError: undefined is not a function". if do API.Getalldata("fenix"); get empty object
have i can get result in the time ?
he's right. No you can't get the result in "real time."
what I need to use to get in on time ?
get answer , i use var deferred = $q.defer(); in my factory and then deferred.resolve({ data1:data }); and then return deferred.promise; in controller get my obj console.log(API.Getalldata("fenix"));
0

I think I found your problem. You are returning "date" not "data"

angular.module('starter.services', [])

.factory('API',function($http){
var data={};
var token = "****";
return{
    Getalldata : function(name){

     [..edited..]

    });

    return date; // HERE, I THINK IS YOUR PROBLEM
    }
    /*getRankData : function(name,sezin){

        mydata = "kola";
        return mydata;

    }*/
};});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.