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

15 浏览
0 Comments

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

我正在使用嵌套的ng-repeat和一个对象上的过滤器。第一个ng-repeat根据gapHeader对象中的headerId进行过滤。第二个ng-repeat将gapSection和sectionId过滤到相应的headerID中。\n我有一个编辑页面,它位于一个单独的模态窗口中。目的是编辑与子对象的headerID和sectionID相对应的内容。这也有一个单独的控制器。数据通过服务共享。\n我的问题是,我为每个gapSection子对象都有一个按钮,当我将当前section中的$index值传递给服务时,我只得到了对应于第二个ng-repeat的$index?例如,如果我在gapSection的第2个ng-repeat上点击按钮(headerId:2,sectionId:2),我得到的$index是1。我需要一个$index是2,对应于gapSection中子对象的位置。\n是否可能传递对应于gapSection原始未过滤对象中定义的$index的真正$index?对此的任何评论将不胜感激,谢谢!\n对象:\n

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
0 Comments

问题的出现原因是在嵌套的ng-repeat中,需要获取内部ng-repeat的索引,但是使用$index无法得到正确的索引值。解决方法是使用indexOf(obj)方法来获取原始列表中对象的索引值。另外,如果需要访问父级ng-repeat的索引,则更适合使用ng-init而不是$parent.$index。通过在父级ng-repeat上使用ng-init,可以设置一个新的变量来保存父级索引。通过以上方法,可以解决问题并获取到正确的索引值。

0