AngularJs - 在点击上添加活动类的最佳实践(ng-repeat)

时间:2014-01-03 11:27:36

标签: javascript html5 angularjs

我想在列表中单击添加活动类,我尝试了以下代码,但它在我的所有项目上添加了活动类:/:

HTML:

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

Js:

  var filters = [
            {
                'filterId': 1,
                'time': 'last 24 hours',
            },
            {
                'filterId': 2,
                'time': 'all',
            },
            {
                'filterId': 3,
                'time': 'last hour',
            },
            {
                'filterId': 4,
                'time': 'today',
            },
            {
                'filterId': 5,
                'time': 'yersteday',
            }
        ]; 


function selectFilter($scope) {

    $scope.items = ['filters'];
    $scope.selected = $scope.items[0];

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

}

请给我一些帮助。

由于

4 个答案:

答案 0 :(得分:68)

最好的解决方案是通过角度$index来定位它,这是数组中的对象索引/位置;

<强> HTML

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

<强> JS /控制器

var app = angular.module('app', []); 

app.controller('selectFilter', function($scope) {
var filters = [
            {
                'filterId': 1,
                'time': 'last 24 hours',
            },
            {
                'filterId': 2,
                'time': 'all',
            },
            {
                'filterId': 3,
                'time': 'last hour',
            },
            {
                'filterId': 4,
                'time': 'today',
            },
            {
                'filterId': 5,
                'time': 'yersteday',
            }
        ]; 

    $scope.filters = filters;
    $scope.selected = 0;

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

<强> JSFIDDLE

答案 1 :(得分:9)

慢慢回答,这是我得到的(可能会增加一点)

工作演示: http://jsfiddle.net/WVY7L/

<强> TEMPLATE

<ul>
    <li ng-repeat="filter in filters" 
       ng-click="select($index)" ng-class="{active: $index===selectedIndex}">
        <span class="filters_ct_status"></span>
        {{filter.time}}
    </li>
</ul>

<强> CONTROLLER

$scope.filters = [
        { filterId: 1, time: 'last 24 hours'},
        { filterId: 2, time: 'all' },
        { filterId: 3, time: 'last hour'},
        { filterId: 4, time: 'today' },
        { filterId: 5, time: 'yersteday'}
    ]; 
$scope.selectedIndex = 0; /* first one set active by default */
$scope.select= function(i) {
  $scope.selectedIndex=i;
};
  • 值得一提的是,在数据中你有一个不应该存在的尾随逗号。

     { filterId: 1, time: 'last 24 hours'**,**}
    

其余的是确保您的控制器传递了数组编号

ng-click="select($index)" ng-class="{active: $index===selectedIndex}"

并且能够保存该数组selectedIndex以便在您的模板中使用

$scope.selectedIndex

ng-class语法

    {active: $index===selectedIndex}

当'$ index'等于'selectedIndex'时,转换为添加名为'active'的类

答案 2 :(得分:2)

我很晚才参加派对,但这是最可重复使用的模式,每次都可以省去重写相同的功能。

<强> HTML:

<ul>
  <li ng-repeat="item in list" ng-click="setActive(item, list)" ng-class="{active: item.active}"></li>
</ul>

<强> JS:

$scope.list = [{}, {}];

$scope.setActive = function(item, list){
  list.some(function(item){
    if(item.active){
      return item.active = false;
    }
  });
  item.active = true;
};

答案 3 :(得分:1)

测试所选项目的ID:

<div class="filters_ct" ng-controller="selectFilter">
    <ul>
        <li ng-repeat="item in filters"  ng-click="select(item)" ng-class="{sel: item.filterId == selected.filterId}">
            <span class="filters_ct_status"></span>
            {{filters.time}}
        </li>
    </ul>
</div>

JS:

var filters = [
            {
                'filterId': 1,
                'time': 'last 24 hours',
            },
            {
                'filterId': 2,
                'time': 'all',
            },
            {
                'filterId': 3,
                'time': 'last hour',
            },
            {
                'filterId': 4,
                'time': 'today',
            },
            {
                'filterId': 5,
                'time': 'yersteday',
            }
        ]; 
    function selectFilter($scope) {

        $scope.filters = filters;
        $scope.selected = $scope.items[0];

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

    }