5
.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)?

Vivz
  • 6,625
  • 2
  • 17
  • 33
subZero
  • 5,056
  • 6
  • 31
  • 51

1 Answers1

8

$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;
});
senshin
  • 10,022
  • 7
  • 46
  • 59
zs2020
  • 53,766
  • 29
  • 154
  • 219
  • What if I just want to return the response data instead and not the whole $http object? Or is this against best practice? Thanks for your help! – subZero Oct 01 '13 at 19:38
  • 2
    @subzero You can switch to use `$resource`, then you should be able to return the `$resouce` object which is a wrapper of promise and then angularjs can binds it automatically to the view. – zs2020 Oct 01 '13 at 19:44