AngularJs $ watch为什么这不起作用?

时间:2013-10-22 15:18:20

标签: javascript angularjs

我正在尝试在这个AngularJs脚本中反向更新值,我在这里写: http://jsfiddle.net/flashpunk/tR5Mr/1/

$scope.$watch('watchList', function(newV,oldV) {
    console.log('change');
    $scope.main.right.assets = addAssets();
    $scope.main.right.roa = roaCalc();
    $scope.main.right.target = targetCalc();
}, true);

您可以看到您可以更改影响MAIN输入框的Slide1,Slide2,Slide3,Slide4和Slide5输入框中的值,但是当MAIN框为时,监视功能似乎不会修改幻灯片框修改。

任何人都可以帮我做这项工作吗?我已经绞尽脑汁待了好几个小时。

2 个答案:

答案 0 :(得分:2)

尝试,

$scope.$watch(function() {
    return $scope.watchList;
}, function (newV, oldV) {
    console.log('change');
    $scope.main.right.assets = addAssets();
    $scope.main.right.roa = roaCalc();
    $scope.main.right.target = targetCalc();
}, true);

Demo

答案 1 :(得分:0)

你的手表正在工作。它是由任何盒子的变化触发的。

问题是您的手表仅在触发时计算主框的值。您需要检测更改的值(幻灯片或主要值),并根据更改的值在适当的方向进行计算..

最简单的解决方案可能是两只手表 - 您目前使用的手表仍在观看所有滑盖。因此,监视列表现在看起来像:

$scope.watchList = [
    $scope.slide1,
    $scope.slide2,
    $scope.slide3,
    $scope.slide4,
    $scope.slide5   
];

这是一款观看主机并更新滑盖的新手表。

$scope.$watch('main', function(newV,oldV) {
    console.log('main changed');
    //Update any slide values that need updating here
}, true);