本文介绍了AngularJs $ watchGroup - 这当然pression组中的最后更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 $ watchGroup
,我怎么能确定前pression组中被更改
$范围。$ watchGroup(['减速','本','月','十六进制'],功能(newValues,oldValues,范围){
last_changed =? //返回该记录在此运行改变了... ...
如果(last_changed =='减速')//那么什么
如果(last_changed =='本')//那么什么
如果(last_changed =='十月')//那么什么
如果(last_changed =='十六进制')//那么什么
});
结果
编辑1: -
结果--->问题是我无法从newValues取消绑定n_vals。
VAR n_vals = [];
$范围。$ watchGroup(['减速','本','月','十六进制'],功能(newValues,oldValues,范围){
LAST_UPDATED = changed_one(newValues,n_vals); // LAST_UPDATED EXP
n_vals = newValues;
});
解决方案: -
n_vals = angular.copy(newValues);
看下面的链接:结果
在角
解决方案
角报价 $ watchGroup
(自1.3)
基本上它返回的范围变量属性oldValue&安培;为newValue的方式如何,我们在推它 watchGroup
。
使用范围变量的指数来获得其相应的newval和OLDVAL像下面
有关获得的 newValues各自范围变量
newValues [0]
为newValue [1]
newValues [2]
newValues [3]
有关获得的 oldValues各自范围变量
oldValues [0]
oldValues [1]
oldValues [2]
oldValues [3]
$范围。$ watchGroup(['十二月','本','十月','十六进制'],功能(newValues,oldValues,范围){
的console.log(newValues [指数]); //把指数值为0(DEC),1(槽),2(OCT)或3(十六进制)
的console.log(oldValues [指数]); //把指数值为0(DEC),1(槽),2(OCT)或3(十六进制)
});
Edit 1
To catch which variable has updated last, You can try below code.
var arrayOfValues = ['dec', 'bin', 'oct', 'hex'];
$scope.$watchGroup(arrayOfValues, function(newValues, oldValues, scope) {
var last_changed;
if (newValues[0] != oldValues[0]) //or simply consider it as 'dec'
last_changed = arrayOfValues[0];
if (newValues[1] != oldValues[1]) //or simply consider it as 'bin'
last_changed = arrayOfValues[1];
if (newValues[2] != oldValues[2]) //or simply consider it as 'oct'
last_changed = arrayOfValues[2];
if (newValues[3] != oldValues[3]) //or simply consider it as 'hex'
last_changed = arrayOfValues[3];
//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
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 2:
I would simply like to minimized the code like below.
var arrayOfValues = ['dec', 'bin', 'oct', 'hex'];
//generic method which will tell you which variable has been updated
$scope.checkWhichVariableHasUpdated = fucntion(watchGroupList, newValuesObj, oldValuesObj) {
var _lastUpdatedValue;
angular.forEach(watchGroupList, function(value, index) {
if (newValuesObj[index] != oldValuesObj[index])
_lastUpdatedValue = value;
});
return _lastUpdatedValue;
};
$scope.mySwitchFunction = function(changedVariable) {
switch (changedVariable) {
case 'dec':
//do something when dec
break;
case 'bin':
//do something when bin
break;
case 'oct':
//do something when oct
break;
case 'hex':
//do something when hex
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 believe now you are more cleared on what you want.
Thanks.
这篇关于AngularJs $ watchGroup - 这当然pression组中的最后更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!