maria
maria

Reputation: 159

angularjs service and response

why is my $scope.aux empty, while the response is acually there?

response:

enter image description here

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

Answers (2)

Akash Bhandwalkar
Akash Bhandwalkar

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

Silvinus
Silvinus

Reputation: 1445

Because getfirm return a promise. To get data try this :

testservice.getfirm().then(function(value){ $scope.aux = value });

Upvotes: 2

Related Questions