Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning response data from HTTP in Angular factory

.factory('Api', function($http) {
         var API = "http://127.0.0.1:4567/";
         return {
             get: function(method) {
                 return $http.get(API + method).success(function(result) {
                     return result;
                 });
             }
         }
     }

Then

console.log(Api.get("MAppData"));

Returns

Object {then: function, success: function, error: function}

Why does it not return the result (response data)?

like image 303
subZero Avatar asked Oct 01 '13 17:10

subZero


1 Answers

$http returns a promise and you need to chain .then() to get the data like this:

Api.get("MAppData").then(function(response){
    var data = response.data;
});
like image 196
zs2020 Avatar answered Nov 15 '22 03:11

zs2020