How do I return data from a $http.get() inside a factory in angularjs

前端 未结 3 1793
孤独总比滥情好
孤独总比滥情好 2020-12-03 11:40

I\'m having a lot of trouble with this and I can\'t seem to find anything here on SO or Google that helps me spot what I\'m doing wrong

         


        
3条回答
  •  没有蜡笔的小新
    2020-12-03 12:11

    Use defer:

    obj.getResponse = function(){                 
        var temp = {};
        var defer = $q.defer();
        $http.get('hello.php').success(function(data){
                alert(data);
                temp =data;
                defer.resolve(data);
    
        });
        return defer.promise;
    }
    

提交回复
热议问题