javascript - removeClass alike logic in angularjs -


.. ng-repeat <li ng-class="{'active':selindex==$index}"> <span ng-click="selindex=$index">click</span> </li> 

the problem above code is, doesn't reset li's class before adding active class clicked one. how apply removeclass logic in angluarjs?

you don't actively remove class in angular. tell angular under circumstances class must active, , figure out. ng-class, do. if condition true, class present on element, else won't. whenever state changes, angular recalculates classes elements. classes removed angular condition becomes false.

more logic doesn't work somewhere. wouldn't use $index anything, since index of 1 item might change @ time. rather use 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 tricky in terms of scope; using function on controller typically works without issues.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -