Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.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
Javascript $scope.$watch在属性更新时不触发_Javascript_Angularjs - Fatal编程技术网

Javascript $scope.$watch在属性更新时不触发

Javascript $scope.$watch在属性更新时不触发,javascript,angularjs,Javascript,Angularjs,以下是观察者声明: $scope.$watch($scope.currentStep , function(newVal , oldVal){ console.log('Watch: ' ,$scope.currentStep , newVal , oldVal); }); 这是唯一更改currentStep属性的代码,这些功能在浏览器中单击按钮时触发: $scope.next = function(valid , event){ event.preventDefault();

以下是观察者声明:

$scope.$watch($scope.currentStep , function(newVal , oldVal){
    console.log('Watch: ' ,$scope.currentStep , newVal , oldVal);
});
这是唯一更改currentStep属性的代码,这些功能在浏览器中单击按钮时触发:

$scope.next = function(valid , event){
    event.preventDefault();

    if(valid){
        $scope.error = false;

         $scope.validate();

        if($scope.currentStep < $scope.totalSteps && !$scope.error){
            $scope.previousStep = $scope.steps.indexOf(true);
            $scope.currentStep = $scope.steps.indexOf(true) + 1;

            $scope.steps[$scope.previousStep] = false;
            $scope.steps[$scope.currentStep] = true;                       
        }
    }
    else {
        $scope.error = true;
        $scope.errorText ="Please fix your mistakes";
    }
}

$scope.prev = function(){
    $scope.error = false;
    $scope.final = false;
    $scope.lastPush --;

    $scope.previousStep = $scope.steps.indexOf(true);
    $scope.currentStep = $scope.steps.indexOf(true) - 1;

    $scope.steps[$scope.previousStep] = false;
    $scope.steps[$scope.currentStep] = true;            
}
$scope.next=函数(有效,事件){
event.preventDefault();
如果(有效){
$scope.error=false;
$scope.validate();
如果($scope.currentStep<$scope.totalSteps&&!$scope.error){
$scope.previousStep=$scope.steps.indexOf(true);
$scope.currentStep=$scope.steps.indexOf(true)+1;
$scope.steps[$scope.previousStep]=false;
$scope.steps[$scope.currentStep]=true;
}
}
否则{
$scope.error=true;
$scope.errorText=“请纠正错误”;
}
}
$scope.prev=函数(){
$scope.error=false;
$scope.final=false;
$scope.lastPush--;
$scope.previousStep=$scope.steps.indexOf(true);
$scope.currentStep=$scope.steps.indexOf(true)-1;
$scope.steps[$scope.previousStep]=false;
$scope.steps[$scope.currentStep]=true;
}

我不明白的是,不管我怎么做,手表只会在变量初始化时启动。当currentStep更新时,手表会错过它。我尝试将第三个参数包含在watch中,以迫使观察者通过相等而不是引用进行比较,但这并不能解决问题。这里缺少什么?

$watch
的第一个参数采用表达式或函数:

$scope.$watch('currentStep' , function(newVal , oldVal){
    console.log('Watch: ' ,$scope.currentStep , newVal , oldVal);
});
是的,你可以使用一个函数

$scope.$watch(function(){ return $scope.currentStep } , function(newVal , oldVal){
    console.log('Watch: ' ,$scope.currentStep , newVal , oldVal);
});
但这显然更冗长,更不可取


根据文档,您的
$watch
表达式必须是
字符串
函数

以下任一项都应起作用:

//字符串
$scope.$watch('currentStep',函数(newVal,oldVal){
console.log('Watch:(当前)%o(新)%o(旧)%o',$scope.currentStep,newVal,oldVal);
});
//作用
$scope.$watch(函数(){
返回$scope.currentStep;
},函数(newVal,oldVal){
console.log('Watch:(当前)%o(新)%o(旧)%o',$scope.currentStep,newVal,oldVal);
});

您的问题似乎已经解决了。你介意接受回答吗?答应我,如果不是我的,我不会生气!