AngularJS - source index of an object in filtered ng-repeat

I use nested ng-repeat and a filter for the object. The first ng-repeat is a filter for the headerId in the gapHeader object. The second ng-repeat filters gapSection, sectionId for the corresponding ID header.

I have an edit page that is in a separate modal window. The goal is to edit the content corresponding to the headerID and sectionID of the sub-object). It also has a separate control. Data is transmitted through the service.

My problem. I have a button for each gapSection subsector that opens the edit page mod, when I pass the value of $ index for the current section in each section of the service, do I get the index $, only the corresponding second ng-repeat? For example, if I press the 2 ng-repeat button on gapSection (headerId: 2, sectionId: 2), I get index $ 1. I require $ index of 2, which matches the position of the sub-object in gapSection,

Is it possible to pass a true $ index that matches the index $ defined in the original unfiltered gapSection? Please rate any comments about this and thank you!

An object:

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}] }; 
+8
angularjs
source share
1 answer

If you need a true index, you don’t even need to pass the $index property, just pass the object and get the index from the source list.

i.e

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

It is also not clear that your problem can really be a ng-repeat nested problem, because according to you, gapSection is an internal ng-repeat and you call a call from internal ng-repeat and need gapSection's index, It should just be available, but there should be a DOM filter will just overload the elements and its index, which you can also get by doing ng-init , i.e. look like ng-init="actIndex=$index" and use actIndex .

If you are trying to access the parent index ng-repeat, then ng-init is more suitable than $ parent. $ index. Since ng-init is specifically designed for this, you must write ng-init=""parentIndex=$index" on parent ng-repeat and use parentIndex.

+13
source share

All Articles