You can listen to the $ routeChangeSuccess event (see the documentation for $ route ) to detect changes.
Reformat your controller and html as follows:
$scope.navList = [ { url: '/route1', title: 'Route 1'}, { url: '/route2', title: 'Route 2'}, { url: '/route3', title: 'Route 3'} ]; <ul> <li ng-repeat="item in navList"> <a ng-class="{active:item.active}" href="#{{item.url}}">{{item.title}}</a> </li> </ul>
And then listen to the changes (in your controller):
function detectRoute() { angular.forEach($scope.navList, function(item) { item.active = $location.path().match(new RegExp(item.url)) ? true : false; }); } $scope.$on('$routeChangeSuccess', detectRoute);
See an example in Plunker .
I wrapped this in a module: https://github.com/Espesen/angular-simple-navbar
source share