Reputation: 159
why is my $scope.aux
empty, while the response is acually there?
response:
I have the following in the factory:
o.getfirm = function() {
return $http.post('/firms', null, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).then(function(response){
console.log("getting firm");
console.log(response.data);
return response.data;
});
};
caller:
$scope.aux = testservice.getfirm();
console.log("checking");
console.log($scope.aux);
Upvotes: 1
Views: 38
Reputation: 901
You can use $q library to handle error also
o.getfirm = function() {
var deferred = $q.defer();
return $http.post('/firms', null, {
headers: {Authorization: 'Bearer '+auth.getToken()}
})
.then(function(response){
console.log("getting firm");
deferred.resolve(response.data);
return response.data;
})
.catch(function(error){
deferred.reject(error);
});
return deferred.promise();
};
And controller:
testservice.getfirm().then(function(value){ $scope.aux = value })
.catch(function(error){//Handle error})
Upvotes: 1
Reputation: 1445
Because getfirm return a promise. To get data try this :
testservice.getfirm().then(function(value){ $scope.aux = value });
Upvotes: 2