简体   繁体   English

AngularJS过滤器累积数组

[英]Angularjs filter cumulative array

I'm having an issue filtering my result sets in an ng-repeat. 我在ng-repeat中过滤结果集时遇到问题。 The filter gets run on each iteration through the ng-repeat cycle (so if I have 10 dispatches returned by currentDispatch.dispatches it will run 10 times). 过滤器会在ng-repeat循环的每次迭代中运行(因此,如果currentDispatch.dispatches返回10个分派,它将运行10次)。 The first time though, runs will only have 1 element. 不过,第一次runs只包含1个元素。 The next time through it will have 2, then 3, and so forth. 下次通过它将有2个,然后是3个,依此类推。 What the results look like on my screen is.... 结果在我的屏幕上是什么样的...

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

How can i make it so it only runs the filter once as it has all the elements to filter against? 我如何才能使其仅运行一次过滤器,因为它具有所有要过滤的元素? I need only the final result set, not a result of every iteration through. 我只需要最终结果集,而不需要每次迭代的结果。

my custom filter 我的自定义过滤器

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
    }
}])

my ng-repeat clause 我的ng-repeat子句

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

I will take the track by $index out once I fix the issue at hand. 解决手头的问题后,我将按照$ index进行跟踪。

Ng-repeat does watch for itself , maybe u are getting data through a ajax request. Ng-repeat会自动监视,也许您是通过ajax请求获取数据的。 Having said that just mask your ng-repeat with ng-if with the condition that it has all 10 elements... A dirty way right. 话虽这么说,但要用ng-if屏蔽ng-repeat,前提是它必须包含所有10个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM