AngularJS通知$watchCollection$watch手表的已更改型号



我有一个$watch在监听一个输入,调用一个服务来获取一些新数据。$watchCollection也需要返回的数据。

我如何通知$watchCollection发生了$watch(从而更新范围)

我的HTML

<div ng-controller="MainCtrl">
<p>A: <input type="text" ng-model="input"></p>
<p>B: <input type="text" ng-model="output"></p>
</div>

我的JS

angular.module('testapp', [])
.controller('MainCtrl', ['$scope', function($scope) {
$scope.$watch('input', function(newVal, oldVal){
//go get some data from a service...
var reallyNewVal = Date.now();
$scope.input = reallyNewVal;
});
$scope.$watchCollection('[output]', function(output){
//watch this, plus some other stuff too, omitted for simplicity
$scope.output = $scope.input - 1391754634457;
});
}]);

JSfiddle示例:http://jsfiddle.net/gC7Zr/

我要寻找的是当$scope.input发生变化时(由于$watch),$watchCollection也会更新。我不想将$scope.input放入$watchCollection,因为它正在处理XHR请求。。

有什么想法吗?

编辑:

下面是我尝试做的另一个例子:http://jsfiddle.net/n46rj/
当您更改B时,C会更新。但是,如果您更改了A,则不会更新C。调用$watch时,如何强制$watchCollection"更新"($digest?)?

由于输出仅依赖于输入,因此可以在第一个$watch中更改它

angular.module('testapp', [])
.controller('MainCtrl', ['$scope', function($scope) {
$scope.$watch('input', function(newVal, oldVal){
//go get some data from a service...
var reallyNewVal = Date.now();
$scope.input = reallyNewVal;
$scope.output = $scope.input - 1391754634457;
});
$scope.$watchCollection('[output]', function(output){
//watch some other stuff
});
}]);

最新更新