如何使用NG-重复什么时候能得到标准化值数组什么时候、数组、如何使用、NG

2023-09-13 04:06:34 作者:cherry(樱桃)

我想获得相关的不同组一个阵列的标准值。

I am trying to get normalized value of one array associated to different groups.

http://jsfiddle.net/k5Dvj/5/

我不希望添加新的东西到原来的数组项,所以我回来了标准化的项目为每个组新的对象。

I don't want to add something new into the original array items, so I am returning new objects for normalized items for each group.

$scope.nomalizedItems = function (groupid) {
    var groupItems = $scope.originalItems.filter(function (item) {
        return item.groupid == groupid
    });

    var values = groupItems.map(function (item) {
        return item.value;
    });
    var maxValue = Math.max.apply(null, values);
    return groupItems.map(function (item) {
        return {
            id: item.id,
            normalizedValue: item.value / maxValue
        };
    });
};

我认为这种逻辑是非常简单的,但是angularjs总是埋怨 [$ rootScope:infdig] 10 $摘要()迭代达到中止!即使我通过item.id 轨中的NG-重复前pression已经添加了。

I believe this logic is really simple, however angularjs is always blaming "[$rootScope:infdig] 10 $digest() iterations reached. Aborting!" even if I have added the "track by item.id" in the ng-repeat expression.

不知道如何解决这个问题呢?谢谢!

Any idea how to solve this issue? Thanks!

推荐答案

ngRepeat 指令不开心。您code创建新对象每次。消化循环一遍又一遍......

ngRepeat directive is not happy. Your code creates new objects each time. Digestion loops over and over again...

据我所知,由前pression 轨道是不应该做的角度匹配相应的新的previous实体后删除通过重新创建实体的所有内部 $$ hashKey 属性。该指令旨在告诉 ngRepeat 指令如何建立这个内部 $$ hashKey 来避免DOM元素创建的浪费。

From what I know, the track by expression is not supposed to make angular match the previous entities with the corresponding new ones after you erased all their internal $$hashKey properties by recreating the entities. This instruction is designed to tell ngRepeat directive how to build this internal $$hashKey to avoid DOM element creation waste.

所以..你可能只是改变而不是创建新的您的项目:

So .. You may only alter your items instead of creating new ones:

    groupItems.forEach(function (item) {
        item.normalizedValue = item.value / maxValue;
    });
    return groupItems;

然后它的作品。

此外,您的筛选,在每个循环消化发生。出于性能的目的,你可以preFER烤这个阵列中的特定事件/观察者的回调。

Moreover, your filtering is happening at each digest loop. For performance purposes, you may prefer bake this array in specific events/watchers callbacks.

更新

其实,消化不会循环一遍又一遍,如果你烤 ngRepeat 前pression之外这个名单!我建议你​​让每个使用了 ngRepeat 指令创建的子范围控制器和烤这个名单中观察家回调。

Actually, digestion won't loop over and over again if you bake this list outside of the ngRepeat expression ! I suggest you to have a controller per group using the child scopes created by the ngRepeat directive and bake this list in a watcher callback.