V
V
Vetal Matitskiy2015-07-15 02:40:34
Angular
Vetal Matitskiy, 2015-07-15 02:40:34

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

3 answer(s)
A
Arthur, 2015-07-15
Mudrick @mudrick

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>

As for your question about the purchase, then you need to buy and fix the selected product not by category and subcategory indices (this is absolutely wrong), but by the product ID.

_
_ _, 2015-07-15
@AMar4enko

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>

D
DigitalSmile, 2015-07-15
@DigitalSmile

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 question

Ask a Question

731 491 924 answers to any question