[英]AngularJS Update Scope $q
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
? 但是,当集合从另一个$scope
更改时,如何强制arrayOfThings
更新(或重新解析?)?
arrayOfThings can be seen only inside a child scope, so any change of arrayOfThings in a child scope will maintain data-binding anyway. arrayOfThings仅在子作用域内可见,因此在子作用域中对arrayOfThings的任何更改仍将保持数据绑定。 The data-binding has to be resolve manually by $scope.$apply if the arrayOfThing is changed from an event (DOM event, $broadcast, etc) 如果从事件(DOM事件,$ broadcast等)更改了arrayOfThing,则必须通过$ scope。$ apply手动解决数据绑定。
You need to put a watch on the service call thingsService.get in the controller that you want to be notified of a change. 您需要监视要通知更改的控制器中的服务调用事物服务.get。 Like this: 像这样:
$scope.$watch(thingsService.get, function(newVal, oldVal){
$scope.arrayOfThings2 = newVal;
} );
This is assuming you are injecting that service into the new controller. 这是假设您要将服务注入到新控制器中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.