2

So recently I've been updating something that looks like the below:

$scope.arrayOfThings = [];

function setArrayOfThings() {
     thingsService.get().then(function (things) {
         $scope.arrayOfThings = things;
     });
}

$scope.$on('arrayOfThings.add', setArrayOfThings);

To look more like this (using the lesser-known promise integration into bindings...):

$scope.arrayOfThings = thingsService.get();

But how do I force arrayOfThings to update (or re-resolve?) when the collection is changed from another $scope?

Jamie
  • 4,670
  • 5
  • 35
  • 49

2 Answers2

0

arrayOfThings can be seen only inside a child scope, so any change of arrayOfThings in a child scope will maintain data-binding anyway. The data-binding has to be resolve manually by $scope.$apply if the arrayOfThing is changed from an event (DOM event, $broadcast, etc)

bdavidxyz
  • 2,492
  • 1
  • 20
  • 40
0

You need to put a watch on the service call thingsService.get in the controller that you want to be notified of a change. Like this:

$scope.$watch(thingsService.get, function(newVal, oldVal){
  $scope.arrayOfThings2 = newVal;  
} );

This is assuming you are injecting that service into the new controller.

BoxerBucks
  • 3,124
  • 2
  • 21
  • 26
  • Causes a digest exception - also this wouldn't work for methods with parameters? – Jamie Sep 01 '13 at 18:11
  • Hmm. There must be something else going on. I have used this a lot to react to data changes in services. Sure, you can pass parameters into the service call if you want. – BoxerBucks Sep 02 '13 at 18:40