列表元素共享相同的$ index Angular js值?

时间:2016-04-05 05:21:47

标签: javascript angularjs

我正在尝试给班级"活跃"点击每个过滤器/我尝试使用$index,但由于有多个列表项具有相同的$index(因为我也在重复ul),因此多个列表元素会使该类处于活动状态。还有另一种方法吗?

HTML:

<div class="filter-options">
  <ul class="filters" ng-repeat="type in filterTypes" type="type.category">
    <li ng-repeat="filter in type.filters" ng-click="selectFilter($index)" ng-class="{'active': selectedFilter == $index}">{{filter.value | uppercase}}class="glyphicon glyphicon-menu-down"></span></li>
  </ul>
</div>

指令:

return {
    restrict: "E",
    templateUrl: 'scripts/directives/all-filters.html',
    link: function(scope, el, attr) {           
        scope.filterTypes = dummyData.filters;

        scope.selectFilter = function(filterChosen){                
            scope.selectedFilter = filterChosen;
        };
    }
}

1 个答案:

答案 0 :(得分:1)

您可以在参数中发送对象,并在函数中添加一个布尔值,如

<强>标记

<li ng-repeat="filter in type.filters" ng-click="selectFilter(filter)" ng-class="{'active': filter.active}">{{filter.value | uppercase}}class="glyphicon glyphicon-menu-down"></span></li>

<强>指令

scope.selectFilter = function(clickedFilter){                
            clickedFilter.active = true;
        };

希望有所帮助