0

I want to do some arithmetic operation with $index in ng-repeat. The following code is not working:

<tr ng-repeat="item in quotation.items track by $index">
    <td class="text-center"><strong>{{$index++}}</strong></td>
    <td><a href="javascript:void(0);">{{item.item}}</a></td>
    <td>{{item.quantity}}</td>
    <td>{{item.rate}}</td>
    <td>{{item.rate * item.quantity}}</td>
</tr>

How can I solve this?

2
  • 2
    I have a feeling you need $index + 1, as $index++ tries to change the actual value of your loop variable. Commented Mar 15, 2015 at 15:13
  • dont do arithmatic operations on $index... Commented Mar 15, 2015 at 15:17

1 Answer 1

3

You need to use + 1. ++ always modifies the variable, and that never works well when you do that on a loop variable.

$index + 1

And this is the correct syntax for the ng-repeat. You don't need a by $index. $index is created automatically.

<tr ng-repeat="item in quotation.items">
Sign up to request clarification or add additional context in comments.

2 Comments

You're welcome. @entre, Why'd you add by $index back in? It was throwing errors for me, but I'm new to javascript.
Ok, I see now. I had left track in. Taking out track or adding by $index in both work.

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.