javascript - Adding an active state to list item using angular -
i have following html markup:-
<ul class="dropdown-menu"> <li ng-repeat="track in songtracks track $index" ng-class="{active: $index===selectedindex}"> <a ng-click="setsongtrack(track)"> {{track.name}} </a> </li> </ul>
in angular, use this:-
$scope.setsongtrack = setsongtrack; $scope.selectedindex = 0; $scope.index= function(i) { $scope.selectedindex=i; };
which passes dashjs so:-
function setsongtrack(track) { musicplayer.setsongtrack(track); $scope.selectedindex = track; }
musicplayer being instance of dashjs, parses audio tracks. works - bunch of list elements track names, can click them , independently load each track expected. however, need add visual way of informing user track selected, best done way of adding css 'active' class.
currently sets 'active' class first list element, regardless if click , changes track. if click list item, 'active' class removed first list item. need 'active' class on selected track, i.e. selected list item.
now i'm stumped. i've read on lot of similar questions , haven't located can work.
can enlighten me? new angular , so, i'm still bit of noob it.
instead of using index of track mark selected, use track itself. write function trackselected()
takes track, , use apply class ngclass
. eg...
html
<ul class="dropdown-menu"> <li ng-repeat="track in songtracks track $index" ng-class="{ 'active': isselected(track)} "> <a ng-click="setsongtrack(track)" ng-bind="track.name"> </a> </li> </ul>
js
$scope.selectedtrack = null; $scope.setsongtrack = function(track) { musicplayer.setsongtrack(track); $scope.selectedtrack = track; } $scope.isselected = function(track) { return $scope.selectedtrack === track; }
this because may add or remove tracks list in view @ point, , selected index invalid.
Comments
Post a Comment