Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom $index name in ngRepeat

I have a code written in JADE that uses two ngRepeat directives, like this:

table
    tr(ng-repeat="item in items track by $index")
       td: .dropdown
           button.btn.btn-default(data-toggle="dropdown") Choose One
           ul.dropdown-menu
               li(ng-repeat="option in item.options")
                  // I need this $index's value comes from the first ng-repeat above
                  a(ng-click="changeOption(item, option, $index)") 

Tag <a> have a ng-click directive that calls changeOption(item, option, $index)

The problem is that I need $index from the FIRST ng-repeat directive in changeOption() function instead of the second one. Can I change $index to another name or assign it to another variable so that I can access it in the second iteration?

Thank you

like image 447
DennyHiu Avatar asked Dec 31 '15 04:12

DennyHiu


People also ask

How to get the index of an item repeated using ng-repeat?

The $index variable is used to get the Index of an item repeated using ng-repeat directive in AngularJS. This article will explain how to use the $index of the row of HTML Table populated using ng-repeat directive in following two scenarios. 1. Get the Index inside the HTML Table cell using AngularJS. 2.

How to get the index of the row created by Ng-repeat directive?

Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.

What is ngrepeat track by $Index in angular?

The ngRepeat track by $index allows you to specify which items should be repeated using their index number. The ngRepeat is a directive that can be added to the HTML template of an Angular application. It is used for creating lists, and it can take an expression as an argument.

What are the custom start and end points for ngrepeat?

The custom start and end points for ngRepeat also support all other HTML directive syntax flavors provided in AngularJS (such as data-ng-repeat-start, x-ng-repeat-startand ng:repeat-start). Directive Info This directive creates new scope. This directive executes at priority level 1000. This directive can be used as multiElement


Video Answer


1 Answers

No need to use ng-init, use ng-repeat's expression support for key/value access. While you're still using an array, they key will be the actual index.

docs - https://docs.angularjs.org/api/ng/directive/ngRepeat

tr(ng-repeat="(t1, category) in vc.listData track by $index"
   td: .dropdown
       button.btn.btn-default(data-toggle="dropdown") Choose One
       ul.dropdown-menu
           li(ng-repeat="(t2, option) in item.options")
              a(ng-click="changeOption(item, option, t1)") 
like image 110
jusopi Avatar answered Oct 04 '22 19:10

jusopi