AngularJs $ watchGroup - 这当然pression组中的最后更改? [英] AngularJs $watchGroup - which expression in the group was last changed?

查看:225
本文介绍了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(十六进制)
});


  

请注意:


  
  

下面逻辑被考虑到只有一个值将在时间改变。


修改1

要赶上这最后的变量已经更新了,你可以试试下面code。

  VAR arrayOfValues​​ = ['减速','本','月','十六进制'];
    $范围。$ watchGroup(arrayOfValues​​,功能(newValues​​,oldValues​​,范围){
        VAR last_changed;
        如果(newValues​​ [0]!= oldValues​​ [0])//或简单地将其视为十二月'
            last_changed = arrayOfValues​​ [0];
        如果(newValues​​ [1]!= oldValues​​ [1])//或者干脆把它作为'本'
            last_changed = arrayOfValues​​ [1];
        如果(newValues​​ [2]!= oldValues​​ [2])//或简单地将其视为辛'
            last_changed = arrayOfValues​​ [2];
        如果(newValues​​ [3]!= oldValues​​ [3])//或简单地将其视为六角'
            last_changed = arrayOfValues​​ [3];
        //上述逻辑可以简单地避免以下条件逻辑(参考,如果条件面前发表评论以上)
        如果(angular.isDefined(last_changed)){
            //你的逻辑会来这里根据你的问题
            如果(last_changed =='减速')//那么什么
            如果(last_changed =='本')//那么什么
            如果(last_changed =='十月')//那么什么
            如果(last_changed =='十六进制')//那么什么
        }
    });

编辑2:

我只想最小化code像下面。

  VAR arrayOfValues​​ = ['减速','本','月','十六进制'];
//通用的方法,它会告诉你哪些变量已经更新
$ scope.checkWhichVariableHasUpdated =温控功能(watchGroupList,newValues​​Obj,oldValues​​Obj){
    VAR _lastUpdatedValue;
    angular.forEach(watchGroupList,功能(价值指数){
        如果(newValues​​Obj [指数]!= oldValues​​Obj [指数])
            _lastUpdatedValue =价值;
    });
    返回_lastUpdatedValue;
};$ scope.mySwitchFunction =功能(changedVariable){
    开关(changedVariable){
        案十二月:
            //做一些事情的时候减速
            打破;
        案斌:
            //做一些事情的时候斌
            打破;
        案十月:
            //做一些事情的时候十月
            打破;
        案十六进制:
            //做一些事情时,十六进制
            打破;
    }};$范围。$ watchGroup(arrayOfValues​​,功能(newValues​​,oldValues​​,范围){
    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 return the scope variable oldValue & newValue in the manner how we putted it in watchGroup.

Use index of the scope variable to get its respective newVal and oldVal like Below

For getting newValues of respective scope variables

newValues[0] newValue[1] newValues[2] newValues[3]

For getting oldValues of respective scope variables

oldValues[0] oldValues[1] oldValues[2] oldValues[3]

$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 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组中的最后更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆