问题
.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)?
回答1:
$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;
});
来源:https://stackoverflow.com/questions/19121918/returning-response-data-from-http-in-angular-factory