angularjs $http.get to get json not working in the service layer

后端 未结 2 979
既然无缘
既然无缘 2020-12-31 10:19

I am developing an angularjs app as a part of my angularjs learning. I have controllers and from there I am calling service layers.

leagueManager.service(&quo         


        
2条回答
  •  时光说笑
    2020-12-31 10:45

    This should work fine:

    myApp.factory('mainFactory',['$http',function($http){
    
    var mainFactory = {};
    
    mainFactory.getRandomUser = function(){
        var promise;
        if(!promise){
            promise = $http.get('http://api.randomuser.me/').success(function(d){
                return d;   
            });
            return promise;
        }
    };
    
    mainFactory.getRandomImage = function(){
        var promise;
        if(!promise){
            promise = $http.get('http://lorempixel.com/400/200/').success(function(d){
                return d;   
            });
            return promise;
        }
    };
    
    return mainFactory;
    
    }]);
    

提交回复
热议问题