Javascript AngularJs$watchGroup-组中的哪个表达式上次更改?
使用Javascript AngularJs$watchGroup-组中的哪个表达式上次更改?,javascript,angularjs,angularjs-service,angularjs-watch,Javascript,Angularjs,Angularjs Service,Angularjs Watch,使用$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(
$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:-
--->问题是我无法将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;
});
解决方案:-
查看以下链接:角度优惠(自1.3版起) 基本上,它返回范围变量
oldValue
和newValue
,方式与我们在watchGroup
中的方式相同
使用scope变量的索引获取其各自的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[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
要获取相应范围变量的oldValue
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[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',函数(newValues,oldValues,scope){
log(newValues[index]);//将索引值设置为0(dec)、1(bin)、2(oct)或3(hex)
log(oldValues[index]);//将索引值设置为0(dec)、1(bin)、2(oct)或3(hex)
});
注意:下面的逻辑是考虑一次只改变一个值 编辑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)
}
});
我希望这能澄清问题。@Ahmedbadaw我更新了我的答案。希望现在它能澄清你的疑虑。感谢你的努力。但是如果我以相反的顺序填写arrayOfValues,则上述代码很容易被破坏。如果这对您有帮助,请将答案标记为正确。谢谢。)很抱歉如果我在(hex)之后更改(dec)的值会怎么样。这将输出(十六进制)作为最后更改的值,尽管这不是真的对不起!我没看见。除了为每个元素创建一个$watchGroup之外,还有其他方法可以实现$watchGroup吗。