ajmajmajma
ajmajmajma

Reputation: 14216

getting 2 levels of parents index in angular

I have a triple nested ng-repeat which I am tyring to allow the user to delete items inside when they are displayed using a simple .splice. I can send the index of the highest level item, the $parent.$index for the next level down, But on the third level down I need somehting like $parent.$parent.$index to pass the index of the correct item int he json object to delete, can I do something like this?

Here's what I tried

ng-click="deleteMe($parent.$parent.$index, $parent.$index, $index) 

How could I properly send the index of the highest parent? Thanks!

Upvotes: 0

Views: 136

Answers (1)

sylwester
sylwester

Reputation: 16498

Use ng-init please see more here https://docs.angularjs.org/api/ng/directive/ngInit

or sample demo here http://plnkr.co/edit/qRJlAzPfwaZr8NENs7K0?p=preview

<body ng-controller="MainCtrl">
  <div ng-repeat="father in data" ng-init="indxFirst = $index">
    <h3>{{father.person}}</h3>
    <ul>
      <li ng-repeat="kids in father.kids" ng-init="indxSecond = $index">{{kids.name}}
        <p ng-repeat="color in kids.colors" ng-init="indxThird=$index">Colour: {{color.color}}
          <button ng-click="delete(indxFirst, indxSecond, indxThird)">delete</button>
          <p>
      </li>
    </ul>
  </div>
</body>

Upvotes: 1

Related Questions