Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs $watch回调执行顺序_Angularjs_Callback - Fatal编程技术网

Angularjs $watch回调执行顺序

Angularjs $watch回调执行顺序,angularjs,callback,Angularjs,Callback,以下是我所拥有的: $scope.$watch('someVar', function (val) { $scope.someList.push(val); execLongFunction.then(function (data) { val.computedValue = data; }) }) 我想要的是:在我的作用域中向数组添加一个值,然后执行一些计算,然后(可能)更新作用域。主要的一点是,我希望在execLongFunction完成之前,将va

以下是我所拥有的:

$scope.$watch('someVar', function (val) {
    $scope.someList.push(val);
    execLongFunction.then(function (data) {
        val.computedValue = data;
    })
})
我想要的是:在我的作用域中向数组添加一个值,然后执行一些计算,然后(可能)更新作用域。主要的一点是,我希望在
execLongFunction
完成之前,将
val
推送到scope数组

问题是:所有$watch回调代码都会立即执行:即使我添加了一些console.log(),我也会同时看到它们

我做错了什么

$scope.$watch('someVar', function (newVal,oldVal) {
    if(newVal != oldVal){
      $scope.someList.push(newVal);
      execLongFunction.then(function (data) {
        val.computedValue = data;
      })
    }
});

$watch返回正在监视的$scope变量的oldValue和newValue。尝试使用它们并相应地执行您的函数。如果“someVar”的值在每次从指令更新时都没有更改,则此操作应该有效。

您是否在持续更新
someVar
。?否,它是由侦听用户操作(文件上载)的指令更新的。someVar的值是否每次都会更改?