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
?
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)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.