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.

Joe
  • 4,274
  • 32
  • 95
  • 175
  • XHR are asynchronous in nature therefore your second log executes before the first one. – AlwaysALearner Sep 18 '13 at 07:36
  • Your request is asyn in nature, therefore you get undefined. – Chandermani Sep 18 '13 at 07: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: http://stackoverflow.com/questions/13033118/angular-js-how-to-bind-to-promises – Sergiu Paraschiv Sep 18 '13 at 08:09

0 Answers0