Joe
Joe

Reputation: 4254

Promises, make available on watch, angular JS

I have a problem with understanding promises.

  $scope.$watch('selectedPipe', function() {
    $scope.sizesFromPipes = test.getSizes($scope.selectedPipe.pipe_id);

    $scope.sizesFromPipes.then(function(sizes){
      $scope.selectedSize = sizes[0]; //Working
      $scope.calculationResults = CalculationFactory.mainCalculation(sizes); 
      console.log($scope.calculationResults) //Working
    });
    console.log($scope.calculationResults) //Is not getting updated, binded view is not getting updated either. 
  });

I have a view that listens on calculationResults. It works once when the app is loaded. But it's not getting updated outside when the watch triggers. How do I make calculationResults update "outside" so my view can access it?

Upvotes: 2

Views: 320

Answers (1)

Josh
Josh

Reputation: 44916

A promise runs asyncronously, so your console output will fire before the promise actually finishes.

Here is a quick example showing the timing of a promise and properties being set on the $scope.

http://jsfiddle.net/jwcarroll/NNgw6/

Update:

I've created another example to try and show promises resolving at different times and how that shows up in the bindings.

Upvotes: 1

Related Questions