You don't actively remove a class in Angular. You tell Angular under which circumstances which class must be active, and it will figure it out. You do that with ng-class
, as you do. If the condition is true
, the class will be present on the element, else it won't. Whenever any state changes, Angular recalculates all the classes for all the elements. Classes will be removed by Angular as soon as the condition for it becomes false
.
More likely your logic doesn't work somewhere. I wouldn't use $index
for anything, since the index of any one item might change at any time. Rather use the data itself:
$scope.items = [{ id: 1 }, { id: 2 }];
$scope.activeItem = null;
$scope.selectItem = function (item) {
$scope.activeItem = item;
}
<li ng-repeat="item in items" ng-class="{ active: item.id == activeItem.id }">
<span ng-click="selectItem(item)">
Using direct assignment inside ng-click
directives can get tricky in terms of scope; using a function on the controller typically works without any issues.