I could not find something to help me solve this simple problem in Angular. All answers are relevant for navigation bars when comparing against the location path.
I built a dynamic table using list and ngRepeat . When I click on a line, I try to assign the selected css class to this line to emphasize the fact that this line was selected by the user and remove .selected from the previously selected line.
I am missing a binding method between the selected string and the CSS class assignment.
I applied each row ( ul ) ng-click="setSelected()" But I do not have enough logic inside the function to apply the changes.
My Code - Plunk
My code is:
var webApp = angular.module('webApp', []); //controllers webApp.controller ('VotesCtrl', function ($scope, Votes) { $scope.votes = Votes; $scope.statuses = ["Approved","Pending","Trash","Spam"]; $scope.setSelected = function() { console.log("show"); } }); //services webApp.factory('Votes', [function() { //temporary repository till integration with DB this will be translated into restful get query var votes = [ { id: '1', created: 1381583344653, updated: '222212', ratingID: '3', rate: 5, ip: '198.168.0.0', status: 'Pending', }, { id: '111', created: 1381583344653, updated: '222212', ratingID: '4', rate: 5, ip: '198.168.0.1', status: 'Spam' }, { id: '2', created: 1382387322693, updated: '222212', ratingID: '3', rate: 1, ip: '198.168.0.2', status: 'Approved' }, { id: '4', created: 1382387322693, updated: '222212', ratingID: '3', rate: 1, ip: '198.168.0.3', status: 'Spam' } ]; return votes; }]);
My HTML:
<body ng-controller='VotesCtrl'> <div> <ul> <li class="created"> <a>CREATED</a> </li> <li class="ip"> <b>IP ADDRESS</b> </li> <li class="status"> <b>STATUS</b> </li> </ul> <ul ng-repeat="vote in votes" ng-click="setSelected()"> <li class="created"> {{vote.created|date}} </li> <li class="ip"> {{vote.ip}} </li> <li class="status"> {{vote.status}} </li> </ul> </div> </body>
My CSS (selected class only):
.selected { background-color: red; }