簡體   English   中英

AngularJS過濾器累積數組

[英]Angularjs filter cumulative array

我在ng-repeat中過濾結果集時遇到問題。 過濾器會在ng-repeat循環的每次迭代中運行(因此,如果currentDispatch.dispatches返回10個分派,它將運行10次)。 不過,第一次runs只包含1個元素。 下次通過它將有2個,然后是3個,依此類推。 結果在我的屏幕上是什么樣的...

[1],[1],[1,3],[1,3,4]

我如何才能使其僅運行一次過濾器,因為它具有所有要過濾的元素? 我只需要最終結果集,而不需要每次迭代的結果。

我的自定義過濾器

angular.module('prototype').filter('dispatchedForMyEquipment', ['Config', function(Config){
    return function(runs){
        var result = []
        var myEquipment = Config.getEquipment();
        runs.forEach(function(run, index, array){
            myEquipment.forEach(function(dispatchEquipment, index, array){
                if (run.units.indexOf(dispatchEquipment)!= -1){
                    result.push(run);
                    return
                }
            });         
        })
        return result
    }
}])

我的ng-repeat子句

<tr ng-repeat-start="dispatch in ($state.current.data.latest ?
 (currentDispatch.dispatches | orderBy: '-time' | dispatchedForMyEquipment)
 : (currentDispatch.dispatches | orderBy: '-time'))  track by $index">

解決手頭的問題后,我將按照$ index進行跟蹤。

Ng-repeat會自動監視,也許您是通過ajax請求獲取數據的。 話雖這么說,但要用ng-if屏蔽ng-repeat,前提是它必須包含所有10個元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM