AngularJs $watchGroup - 最后更改组中的哪个表达式?
AngularJs $watchGroup - which expression in the group was last changed?
使用$watchGroup
,如何确定组中的哪个表达式已更改
$scope.$watchGroup(['dec','bin','oct','hex'], function(newValues, oldValues, scope) {
last_changed = ??? //return which exp was changed in this run...
if(last_changed=='dec') //then something
if(last_changed=='bin') //then something
if(last_changed=='oct') //then something
if(last_changed=='hex') //then something
});
编辑 1:-
--->问题是我无法从 newValues 中解除 n_vals 的绑定。
var n_vals = [];
$scope.$watchGroup(['dec','bin','oct','hex'], function(newValues, oldValues, scope) {
last_updated = changed_one(newValues,n_vals); //last_updated EXP
n_vals = newValues;
});
解决方法:-
n_vals = angular.copy(newValues);
看下面的link:
Deep copying objects in angular?
Angular 优惠 $watchGroup
(自 1.3 起).
基本上,它 returns 作用域变量 oldValue
和 newValue
就像我们把它放在 watchGroup
.
中的方式
使用范围变量的索引获取其各自的 newVal
和 oldVal
,如下所示。
获取各个作用域变量的 newValue
s:
newValue[0] // returns new value of dec
newValue[1] // returns new value of bin
newValue[2] // returns new value of oct
newValue[3] // returns new value of hex
获取各个作用域变量的oldValue
s:
oldValue[0] // returns old value of dec
oldValue[1] // returns old value of bin
oldValue[2] // returns old value of oct
oldValue[3] // returns old value of hex
$scope.$watchGroup(['dec','bin','oct','hex'], function(newValues, oldValues, scope) {
console.log(newValues[index]); //put index value as 0(dec), 1(bin), 2(oct) or 3(hex)
console.log(oldValues[index]); //put index value as 0(dec), 1(bin), 2(oct) or 3(hex)
});
NOTE:
Below logic is considering that only one value will change at a time.
编辑 1
要获取最后更新的变量,您可以尝试以下代码。
var arrayOfValues = ['dec', 'bin', 'oct', 'hex'];
$scope.$watchGroup(arrayOfValues, function(newValues, oldValues, scope) {
let last_changed;
var chanedIndex = newValues.findIndex(function(val, key){
return val !== oldValues[key];
});
last_changed = arrayOfValues[chanedIndex];
// above logic could simply avoid the below conditional logic (refer
// comment above in front of if condition)
if (angular.isDefined(last_changed)) {
// your logic will come here as per your question
switch(last_changed){
case 'dec':
doSomething();
case 'bin':
doSomething();
case 'oct':
doSomething();
case 'hex':
doSomething();
}
}
});
编辑 2:
我缩小了代码并制作了
var arrayOfValues = ['dec', 'bin', 'oct', 'hex'];
// generic method which will tell you which variable has been updated
$scope.checkWhichVariableHasUpdated = fucntion(watchGroupList, newValuesObj, oldValuesObj) {
let _lastUpdatedValue;
angular.forEach(watchGroupList, function(value, index) {
if (newValuesObj[index] != oldValuesObj[index])
_lastUpdatedValue = value;
});
return _lastUpdatedValue;
};
$scope.mySwitchFunction = function(changedVariable) {
switch (changedVariable) {
case 'dec':
doSomething();
break;
case 'bin':
doSomething();
break;
case 'oct':
doSomething();
break;
case 'hex':
doSomething();
break;
}
};
$scope.$watchGroup(arrayOfValues, function(newValues, oldValues, scope) {
var last_changed = $scope.checkWhichVariableHasUpdated(arrayOfValues, newValues, oldValues);
if (angular.isDefined(last_changed)) {
$scope.mySwitchFunction(last_changed)
}
});
我希望这能解决问题。
使用$watchGroup
,如何确定组中的哪个表达式已更改
$scope.$watchGroup(['dec','bin','oct','hex'], function(newValues, oldValues, scope) {
last_changed = ??? //return which exp was changed in this run...
if(last_changed=='dec') //then something
if(last_changed=='bin') //then something
if(last_changed=='oct') //then something
if(last_changed=='hex') //then something
});
编辑 1:-
--->问题是我无法从 newValues 中解除 n_vals 的绑定。
var n_vals = [];
$scope.$watchGroup(['dec','bin','oct','hex'], function(newValues, oldValues, scope) {
last_updated = changed_one(newValues,n_vals); //last_updated EXP
n_vals = newValues;
});
解决方法:-
n_vals = angular.copy(newValues);
看下面的link:
Deep copying objects in angular?
Angular 优惠 $watchGroup
(自 1.3 起).
基本上,它 returns 作用域变量 oldValue
和 newValue
就像我们把它放在 watchGroup
.
使用范围变量的索引获取其各自的 newVal
和 oldVal
,如下所示。
获取各个作用域变量的 newValue
s:
newValue[0] // returns new value of dec
newValue[1] // returns new value of bin
newValue[2] // returns new value of oct
newValue[3] // returns new value of hex
获取各个作用域变量的oldValue
s:
oldValue[0] // returns old value of dec
oldValue[1] // returns old value of bin
oldValue[2] // returns old value of oct
oldValue[3] // returns old value of hex
$scope.$watchGroup(['dec','bin','oct','hex'], function(newValues, oldValues, scope) {
console.log(newValues[index]); //put index value as 0(dec), 1(bin), 2(oct) or 3(hex)
console.log(oldValues[index]); //put index value as 0(dec), 1(bin), 2(oct) or 3(hex)
});
NOTE:
Below logic is considering that only one value will change at a time.
编辑 1
要获取最后更新的变量,您可以尝试以下代码。
var arrayOfValues = ['dec', 'bin', 'oct', 'hex'];
$scope.$watchGroup(arrayOfValues, function(newValues, oldValues, scope) {
let last_changed;
var chanedIndex = newValues.findIndex(function(val, key){
return val !== oldValues[key];
});
last_changed = arrayOfValues[chanedIndex];
// above logic could simply avoid the below conditional logic (refer
// comment above in front of if condition)
if (angular.isDefined(last_changed)) {
// your logic will come here as per your question
switch(last_changed){
case 'dec':
doSomething();
case 'bin':
doSomething();
case 'oct':
doSomething();
case 'hex':
doSomething();
}
}
});
编辑 2:
我缩小了代码并制作了
var arrayOfValues = ['dec', 'bin', 'oct', 'hex'];
// generic method which will tell you which variable has been updated
$scope.checkWhichVariableHasUpdated = fucntion(watchGroupList, newValuesObj, oldValuesObj) {
let _lastUpdatedValue;
angular.forEach(watchGroupList, function(value, index) {
if (newValuesObj[index] != oldValuesObj[index])
_lastUpdatedValue = value;
});
return _lastUpdatedValue;
};
$scope.mySwitchFunction = function(changedVariable) {
switch (changedVariable) {
case 'dec':
doSomething();
break;
case 'bin':
doSomething();
break;
case 'oct':
doSomething();
break;
case 'hex':
doSomething();
break;
}
};
$scope.$watchGroup(arrayOfValues, function(newValues, oldValues, scope) {
var last_changed = $scope.checkWhichVariableHasUpdated(arrayOfValues, newValues, oldValues);
if (angular.isDefined(last_changed)) {
$scope.mySwitchFunction(last_changed)
}
});
我希望这能解决问题。