2014-02-07 4 views
0

나는 새로운 데이터를 얻기 위해 입력을 청취하는 $ watch를 가지고 있습니다. 반환 된 데이터는 $ watchCollection에서도 필요합니다.

은 내가 $ watchCollection을 알릴 수 있습니다 어떻게 $ 시계

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

내 JS

에게 (범위 따라서 업데이트)

내 HTML

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이 변경 될 때입니다 (du e to $ watch), $ watchCollection도 업데이트됩니다. $ scope.input을 $ watchCollection에 넣고 싶지 않습니다. XHR 요청을 보내고 있습니다.

아이디어가 있으십니까?

편집 :
당신이 B 변경으로 http://jsfiddle.net/n46rj/는, C가 업데이트 될 때 :

는 여기에 내가 할 노력하고있어 또 다른 예입니다. 그러나 A를 변경하면 C가 업데이트되지 않습니다. 어떻게하면 $ watch가 호출 될 때 $ watchCollection 'update'($ digest?)를 강제로 할 수 있습니까?

+0

에서 변경할 수 있습니까? – michael

+0

정확히, $ scope.output = $ scope.input - 132365413은 원하는 것을 이루기 위해 입력 감시에 있어야합니다 – doodeec

답변

0

출력은 입력에 의존하기 때문에 당신이 $ watchCollection와 OUPUT을보고 왜 첫 번째 $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 

    }); 
}]); 
관련 문제