显示从ng-repeat生成的li上的背景颜色

时间:2018-04-27 10:03:48

标签: jquery html css angularjs

以下代码会动态显示图片和文字列表。

<ul class="presale">
                    <li ng-repeat="(k,v) in value | groupBy : 'presales_name'" ng-click="showData(k.split(' ').join('-').replace('(','').replace(')',''), $index)">
                        <div class="imgDiv">
                            <img src="/images/phones/sub-family/xs/{{k.split(' ').join('-').replace('(','').replace(')','-').toLowerCase()}}.png">
                        </div>
                        <p><a>{{k}}</a></p>
                    </li>
</ul>


$scope.showData = function (item, index) {              

}

我需要添加一个代码来显示用户选择中特定li的背景颜色,然后隐藏前一个选定li的背景颜色。

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作

  

JS档案

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

app.controller('NavigationController', function ($scope) 
{
    // Must use a wrapper object, otherwise "activeItem" won't work
    $scope.states = {};
    $scope.states.activeItem = 'item1';
    $scope.items = [{
        id: 'item1',
        title: 'Home'
    }, {
        id: 'item2',
        title: 'Public Rooms'
    }, {
        id: 'item3',
        title: 'My Rooms'
    }];
});
  

HTML文件

<body ng-app="app">
    <div ng:controller="NavigationController">
        <p>
            Your active menu item: <span class="highlightOne">{{states.activeItem}}</span>
        </p>
        <ul class="navigation">
            <li ng-repeat="item in items" class="item" ng-class="{'active': item.id == states.activeItem}" ng-click="states.activeItem=item.id">{{item.title}}</li>
        </ul>
    </div>
</body>
  

CSS文件

ul.navigation li
{
    background: #f6f6f6;
    padding: 10px;
    margin: 2px 0;
    display: block;
    cursor: pointer;
}

    ul.navigation li.active
    {
        background: blue;
        color: #f6f6f6;
        display: block;
        padding: 10px;
    }

.highlightOne
{
    background: orange;
    font-size: 125%;
    margin: 5px;
    padding: 5px;
}

.navigation
{
    display: block;
    background: #666;
    margin: 10px;
    padding: 10px;
    width: 40%;
}

p
{
    margin: 10px 0 20px;
}

有关代码参考,请查看“https://codepen.io/SkiWether/pen/lzjAq”。