AngularJS - 过滤的ng-repeat中对象的原始索引

Ka *_*ech 9 angularjs

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

我有一个编辑页面,它在一个单独的模态窗口中.目的是编辑对应于子对象的headerID和sectionID的内容.这也有一个单独的控件.数据通过服务共享.

我的问题我有一个按钮用于每个gapSection子对象,它打开编辑页面模态,当我将每个部分中当前部分的$ index值传递给服务时,我得到的$ index仅对应于第二个ng-重复?例如,如果我单击gapSection上的2 ng-repeat内的按钮(headerId:2,sectionId:2),我得到$ 1的索引.我需要$ index为2,它对应于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}]



};
Run Code Online (Sandbox Code Playgroud)

PSL*_*PSL 15

如果您需要真正的索引,甚至不需要传递$index属性,只需传递对象并从原始列表中获取索引.

$scope.gapSectionFn = function(obj){
    var idx = data.gapSection.indexOf(obj);
}
Run Code Online (Sandbox Code Playgroud)

另外还不清楚你的问题是否真的可能是嵌套的ng-repeat问题,因为根据你的情况,gapSection是内部ng-repeat,你正在调用内部ng-repeat和需要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.