, as shown in the demo, but I do not want it to be deleted if I ...">

"ng-class" for each clicked item

I need to add a class to the selected one <li>, as shown in the demo, but I do not want it to be deleted if I click on another <li>.

Please, help

<div ng-app="myApp">
    <ul ng-controller="myCtrl">
        <li ng-repeat="item in model.items" class="commonClass" ng-class="{'on': model.selected==item}" ng-click="model.selected=item">{{ item.name }}</li>
    </ul>
</div>

Js

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

app.controller('myCtrl', function ($scope) {

    $scope.model = {
        selected: null,
        items : [
            {name: "Apple"}, 
            {name: "Banana"}, 
            {name: "California"}
         ]
    };
})

demo

+4
source share
1 answer

Just add like this

<div ng-app="myApp">
    <ul ng-controller="myCtrl">
        <li ng-repeat="item in model.items" class="commonClass" ng-class="{'on': selected==$index}" ng-click="selected=$index">{{ item.name }}</li>
    </ul>
</div>
+2
source

All Articles