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 = ???//返回在这次运行中改变了哪个exp...if(last_changed=='dec')//那么一些东西if(last_changed=='bin')//那么一些东西if(last_changed=='oct')//那么一些东西if(last_changed=='hex')//那么一些东西});
编辑 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 经验值n_vals = 新值;});
解决方案:-
n_vals = angular.copy(newValues);
看下面的链接:
以角度深度复制对象?
Angular 提供 $watchGroup
(自 1.3 起).
基本上,它返回作用域变量 oldValue
和 newValue
,就像我们把它放在 watchGroup
中一样.
使用范围变量的索引来获取其各自的newVal
和oldVal
,如下所示.
获取各个作用域变量的newValue
:
newValue[0]//返回 dec 的新值newValue[1]//返回 bin 的新值newValue[2]//返回 oct 的新值newValue[3]//返回十六进制的新值
获取各个作用域变量的oldValue
:
oldValue[0]//返回 dec 的旧值oldValue[1]//返回 bin 的旧值oldValue[2]//返回 oct 的旧值oldValue[3]//返回十六进制的旧值
$scope.$watchGroup(['dec','bin','oct','hex'], function(newValues, oldValues, scope) {console.log(newValues[index]);//将索引值设为 0(dec)、1(bin)、2(oct) 或 3(hex)console.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) {让 last_changed;var chanedIndex = newValues.findIndex(function(val, key){返回 val !== oldValues[key];});last_changed = arrayOfValues[chanedIndex];//上面的逻辑可以简单地避免下面的条件逻辑(参考//在 if 条件前的注释)如果(angular.isDefined(last_changed)){//你的逻辑会根据你的问题来到这里开关(last_changed){案例'dec':做点什么();案例bin":做点什么();案例八月":做点什么();案例'十六进制':做点什么();}}});
编辑 2:
我已经缩小了代码并制作了
var arrayOfValues = ['dec', 'bin', 'oct', 'hex'];//通用方法,它将告诉您哪个变量已被更新$scope.checkWhichVariableHasUpdated = fucntion(watchGroupList, newValuesObj, oldValuesObj) {让 _lastUpdatedValue;angular.forEach(watchGroupList, function(value, index) {if (newValuesObj[index] != oldValuesObj[index])_lastUpdatedValue = 值;});返回 _lastUpdatedValue;};$scope.mySwitchFunction = function(changedVariable) {开关(改变变量){案例'dec':做点什么();休息;案例bin":做点什么();休息;案例八月":做点什么();休息;案例'十六进制':做点什么();休息;}};$scope.$watchGroup(arrayOfValues, function(newValues, oldValues, scope) {var last_changed = $scope.checkWhichVariableHasUpdated(arrayOfValues, newValues, oldValues);如果(angular.isDefined(last_changed)){$scope.mySwitchFunction(last_changed)}});
我希望这能解决问题.
Using $watchGroup
, how can I determine which expression in the group was changed
$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
});
Edit 1:-
--->the problem is i can't unbind the n_vals from the newValues.
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;
});
Solution:-
n_vals = angular.copy(newValues);
look the following link:
Deep copying objects in angular?
Angular offers $watchGroup
(since 1.3).
Basically, it returns the scope variable oldValue
and newValue
in the manner how we put it in watchGroup
.
Use the index of the scope variable to get its respective newVal
and oldVal
like below.
To get newValue
s of respective scope variables:
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
To get oldValue
s of respective scope variables:
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.
Edit 1
To catch which variable was updated last, you can try below code.
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();
}
}
});
Edit 2:
I have minified the code and made the
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)
}
});
I hope this clears things up.
这篇关于AngularJs $watchGroup - 上次更改组中的哪个表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!