为什么当数字设置为相同值时手表似乎避免触发
Why do watches seem to avoid firing when number is set to same value
我正在尝试做类似于 this plunker 的事情。请注意,我使用 ng-click 事件将数字的值设置回相同的数字。但是,更改似乎并没有触发。
$scope.$watch('value', function(newValue, oldValue){
console.log("Value changed from "+oldValue+" to "+newValue);
});
$scope.call = function(){
console.log("Call is called");
$scope.value=1;
}
所以我将其更改为以下...
$scope.call = function(){
console.log("Call is called");
$scope.value=0;
$scope.value=1;
}
仍然没有看火,但是,this will seem to solve the issue(虽然很乱)
$scope.name = 'World';
$scope.value = {};
$scope.value.value=1;
$scope.$watch('value', function(newValue, oldValue){
console.log("Value changed from "+oldValue.value+" to "+newValue.value);
});
$scope.call = function(){
console.log("Call is called");
$scope.value = {};
$scope.value.value=1;
}
所以问题是,这是怎么回事,为什么我看不到设置为相同值的数字?有更简洁的方法来处理这个问题吗?
此行为是 documented(向下滚动到 $watch 部分)
The listener is called only when the value from the current
watchExpression and the previous call to watchExpression are not equal
(with the exception of the initial run).
Inequality is
determined according to reference inequality, strict comparison via
the !== Javascript operator, unless objectEquality == true ...
我正在尝试做类似于 this plunker 的事情。请注意,我使用 ng-click 事件将数字的值设置回相同的数字。但是,更改似乎并没有触发。
$scope.$watch('value', function(newValue, oldValue){
console.log("Value changed from "+oldValue+" to "+newValue);
});
$scope.call = function(){
console.log("Call is called");
$scope.value=1;
}
所以我将其更改为以下...
$scope.call = function(){
console.log("Call is called");
$scope.value=0;
$scope.value=1;
}
仍然没有看火,但是,this will seem to solve the issue(虽然很乱)
$scope.name = 'World';
$scope.value = {};
$scope.value.value=1;
$scope.$watch('value', function(newValue, oldValue){
console.log("Value changed from "+oldValue.value+" to "+newValue.value);
});
$scope.call = function(){
console.log("Call is called");
$scope.value = {};
$scope.value.value=1;
}
所以问题是,这是怎么回事,为什么我看不到设置为相同值的数字?有更简洁的方法来处理这个问题吗?
此行为是 documented(向下滚动到 $watch 部分)
The listener is called only when the value from the current watchExpression and the previous call to watchExpression are not equal (with the exception of the initial run).
Inequality is determined according to reference inequality, strict comparison via the !== Javascript operator, unless objectEquality == true ...