这是观察者声明:
$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;
}
我不明白的是,无论我做什么,手表只在变量的初始化时触发。当currentStep更新时,手表会错过它。我尝试在watch中包含第三个参数,以强制观察者按相等方式而不是引用方式进行比较,但这不能解决问题。我在这里想念什么?
根据文档,您的$watch
表达式必须为aString
或a 。Function
$rootScope.$watch
以下任何一种都可以工作:
// String
$scope.$watch('currentStep', function(newVal, oldVal) {
console.log('Watch: (current) %o (new) %o (old) %o', $scope.currentStep, newVal, oldVal);
});
// Function
$scope.$watch(function() {
return $scope.currentStep;
}, function(newVal, oldVal) {
console.log('Watch: (current) %o (new) %o (old) %o', $scope.currentStep, newVal, oldVal);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句