在ng-repeat

时间:2015-07-14 09:46:30

标签: angularjs

我有两个迭代,一个列表及其包装器。我想在点击该项目时添加一个类。但现在该类添加了所有包装器列表项。请参阅以下链接。

$scope.select= function(index) {
   $scope.selected = index; 
};

https://jsfiddle.net/9me2L1ev/

请有人帮助我。
感谢
VIMAL

4 个答案:

答案 0 :(得分:2)

你需要做这样的事情,你有逻辑错误。

<div ng-app='app' class="filters_ct" ng-controller="selectFilter">
    <div ng-repeat="filter1 in filters">
    <ul>
        <li ng-repeat="filter in filters" ng-click="filter1[selected] = $index " ng-class="{active: $index == filter1[selected]}">
            <span class="filters_ct_status"></span>
            {{filter.time}}
        </li>
    </ul>
    </div></div>

查看更新的小提琴link

答案 1 :(得分:2)

您需要组合两个$index值。使用parent $index

参考$parent.$index
ng-click="select($parent.$index, $index)" ng-class="{active: $parent.$index + '.' + $index == selected}"

$scope.select= function(parentIndex, index) {
       $scope.selected = parentIndex + '.' + index; 
};

选中此https://jsfiddle.net/56dc8dej/

答案 2 :(得分:0)

I have updated code in his link

<div ng-app='app' class="filters_ct" ng-controller="selectFilter">
<div>
<ul>
    <li ng-repeat="filter in filters" ng-click="select($index)" ng-class="{active: $index == selected}">
        <span class="filters_ct_status"></span>
        {{filter.time}}
    </li>
</ul>
</div></div>

答案 3 :(得分:0)

您需要考虑父索引......

<div ng-app='app' class="filters_ct" ng-controller="selectFilter">
    <div ng-repeat="filter1 in filters">
        <ul>
            <li ng-repeat="filter in filters" ng-click="select($parent.$index, $index)" ng-class="{active: $parent.$index+','+$index == selected }">
                <span class="filters_ct_status"></span>
                {{filter.time}}
            </li>
       </ul>
    </div>
</div>

在你的控制器中:

...
$scope.select= function(pindex, index) {
    $scope.selected = pindex+','+index; 
};
...