2015-02-08 13 views
8

我在对象上使用嵌套的ng-repeat和过滤器。第一个ng-repeat是对gapHeader对象中的headerId的过滤器。第二个ng-repeat过滤器gapSection,sectionId到相应的headerID。AngularJS - 过滤ng-repeat内对象的原始索引

我有一个编辑页面是在一个单独的模式窗口。其目的是编辑对应于该子对象的headerID & sectionID的内容)这也具有单独的控制。数据通过服务共享。

我的问题我有一个按钮为每个gapSection子对象,它打开编辑页面模式,当我将每个部分内的当前部分的$索引值传递给服务时,我得到的$索引只对应于第二个ng-repeat?例如,如果我点击gapSection(headerId:2,sectionId:2)上的2 ng-repeat内的按钮,我会得到1的$ index。我需要一个2的$ index,它对应于gapSection中的子对象位置。

是否可以传递真正的$ index对应于gapSection的原始未过滤对象中定义的$ index?欣赏对此的任何评论,并感谢你!

对象:

var data ={ 
gapHeader:[{headerId:1,name:"General Requiremets",isApplicable:true}, 
        {headerId:2,name:"Insurance",isApplicable:false}], 


gapSection:[{headerId:1,sectionId:1,requirement:"A facility is required to have company structure",finding:null,cmeasures:null,cprocedures:null,personResp:null,isAction:null}, 
     {headerId:2,sectionId:1,requirement:"Organisation must have public liablity",finding:null,cmeasures:null,cprocedures:null,personResp:null,isAction:null}, 
     {headerId:2,sectionId:2,requirement:"Facility must hold workers compensation insurance",finding:null,cmeasures:null,cprocedures:null,personResp:null,isAction:null}] 



}; 
+0

这看起来类似以下内容: [嵌套NG重复和$指数问题] [1] [1]:http://stackoverflow.com/questions/15256600/passing -2-index-values-nested-ng-repeat – Manube 2015-02-08 00:18:16

回答

13

如果你需要,你甚至都不需要通过$index财产,只是传递对象,并从原来的列表索引中的真实指标。

$scope.gapSectionFn = function(obj){ 
    var idx = data.gapSection.indexOf(obj); 
} 

也并不清楚你的问题可能真的是嵌套NG重复的问题,因为根据你gapSection是内NG重复和你调用从内NG-通话重复并需要gapSection's索引。它应该是可用的,但DOM过滤器的存在将重新组织物品及其索引,您也可以通过执行ng-init,即查看ng-init="actIndex=$index"并使用actIndex来获得该索引。

如果您试图访问父级ng-repeat的索引,那么ng-init比$ parent。$ index更合适。由于ng-init是专门为此设计的,所以在父亲ng-repeat中,您将编写ng-init=""parentIndex=$index"并使用parentIndex。

+1

indexOf(obj)完美地工作,谢谢!你对ng重复是正确的,真正的第一个ng重复在另一个对象上工作,所以为什么我最初尝试的$ parent。$ index不起作用,因为我得到了gapHeader而不是GapSection的索引。再次感谢,认为这将比想象的更难! – 2015-02-08 01:15:35

+0

@KaTech不客气。 – PSL 2015-02-08 01:26:50

相关问题