0

I'm new to AngularJS so please be kind to me.. :-P

So I'm looping twice with ng-repeat as in this example:

    <ul>
      <li ng-repeat="b in aMSG">
        <p>{{b.name}}</p>
        <ul>
          <li ng-repeat="c in b.x"><a ng-click="getM($parent.$index,$index)" href="#">{{c.name}}</a></i>
        </ul>
      </li>
    </ul>

See the fiddle: http://jsfiddle.net/trK98/

But when I apply a filter to search for text within the children:

    <ul>
      <li ng-repeat="b in aMSG">
        <p>{{b.name}}</p>
        <input type="text" ng-model="search" placeholder="Search for?">
        <ul>
          <li ng-repeat="c in b.x|filter:search"><a ng-click="getM($parent.$index,$index)" href="#">{{c.name}}</a></i>
        </ul>
      </li>
    </ul>

The $index is lost as you can see here: http://jsfiddle.net/zb2kc/

(search for instance for juice then click on it you'll see $index = 0)

What am I doing wrong?

Thank you!

P.S: Sorry for my poor english.

10
  • it's not lost, because your filtered array has only 1 item and juice is the first item. Commented Feb 8, 2014 at 5:53
  • I see. So how to point to the index in the not filtered array? Commented Feb 8, 2014 at 5:54
  • What do you need the $index for in this case? Commented Feb 8, 2014 at 5:54
  • I need it for another scope to use. Commented Feb 8, 2014 at 5:55
  • @pr.nizar: I don't know why you need to point to the original index. But you could try a trick with ng-show and use the original array. Commented Feb 8, 2014 at 5:55

1 Answer 1

2

Never use $index for any kind of logic. It can be used for managing CSS classes only. It's a highly volatile variable and will be changed after any change in source array (deletion, re-ordering), so $index is not bind to element of array, but only to position of some element in current view rendering.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.