Answer the question
In order to leave comments, you need to log in
How to use multiple $index in nested ng-repeat for angular.js?
Good afternoon, dear web development gurus,
I'm trying to write a small application in angular for an online store. Now in the application, one ng-repeat directive goes through all the products in the category and when the user clicks the button to buy the product index - $index is sent to the controller and in the model the fact of purchase is recorded. I want the application to have the ability to divide products into categories with the ability to send the category number and product number in the category to the controller, but I don’t know if it’s possible to get both indexes in different variables in Angular. I will be grateful for your tips
Answer the question
In order to leave comments, you need to log in
Several $indexes are done this way and nothing else (everything else is some kind of disgusting horror):
<ul>
<li ng-repeat="(i, items) in collection">
<ul>
<li ng-repeat="(j, item) in items">
{{i}} — {{j}}
Чекбокс: <input type="checkbox" name="field[{{i}}][{{j}}]">
<button ng-click="doSomething(i, j)">Клацнуть</button>
<button ng-click="buyItem(item.id)">Купить</button>
</li>
</ul>
</li>
</ul>
Try like this
<div ng-repeat="item in level1" ng-init="level1Index = $index">
<div ng-repeat="item in level2" ng-init="level2Index = $index">
{{level1Index}} - {{level2Index}}
</div>
</div>
It's still possible .
But if suddenly you have nesting more than two cycles, it's better to change the logic of behavior.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question