javascript – 在angularjs中一次选择一个项目

我想在angularJS中一次选择一个项目.

这是我的代码

<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>

问候
瑙曼

最佳答案 ng-repeat从当前范围创建新范围,因此如果您在内部使用选定内部ng-repeat将导致为内部ng-repeat创建新范围变量.

如果要从ng-repeat引用父作用域,则使用$parent.selected,它将引用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

有关更多信息here is link

点赞