0

This is my controller:

ProductsFactory.getOneProduct().then(function(d){
  $scope.selectedProduct = d.data;
  console.log($scope.selectedProduct); //Working
});
console.log($scope.selectedProduct); //Undefined

I can access the data from within the function. But not from outside. This means I cant use the data in my GUI. How do I make access from outside?

Update Apperently it is working in the GUI. But you can not console anything outside.

3
  • XHR are asynchronous in nature therefore your second log executes before the first one. Commented Sep 18, 2013 at 7:36
  • Your request is asyn in nature, therefore you get undefined. Commented Sep 18, 2013 at 7:37
  • That's the way promises work. Your interface should gracefully treat the case when $scope.selectedProduct is undefined. After your async request executes and you update the $scope with new data Angular will know it needs to update the view also. It's been discussed in great detail here: stackoverflow.com/questions/13033118/… Commented Sep 18, 2013 at 8:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.