4

I am trying to select one item on one time in angularJS. Here is my code

<div ng-repeat="list in listgroups" >
    <ul ng-init="selected = -1">
        <li ng-repeat="item in list" ng-class="{active:(selected == $index)}" ng-click="selected = $index;">{{item}} </li>
    </ul>
</div>

Regards Nauman

9me
  • 1,078
  • 10
  • 36

1 Answers1

2

ng-repeat creates new scope from current scope, so If you are using selected inside a inner ng-repeat will cause to create new scope variable for inner ng-repeat.

If you want to refer the parent scope from ng-repeat then use $parent.selected that will refer the outer scope variable of ng-repeat.

HTML

<div ng-repeat="list in listgroups" ng-init="selected = -1">
    <b>List {{$index + 1}}</b>
    <ul>
        <li ng-repeat="item in list" ng-class="{active:($parent.selected == $index)}" 
          ng-click="$parent.selected = $index;">
           {{item}}
        </li>
    </ul>
</div>

Working Fiddle

For more information here is link

Community
  • 1
  • 1
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299