user2420249
user2420249

Reputation: 248

angularjs api , return object

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"));
    }

});

Upvotes: 0

Views: 518

Answers (2)

Vladimir Zdenek
Vladimir Zdenek

Reputation: 2290

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.

Upvotes: 3

Chris L
Chris L

Reputation: 1061

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;

    }*/
};});

Upvotes: 0

Related Questions