As the topic say I want to highlight current menu item with a class “active”, using angular.js.
My html looks like this:
<ul>
<li><a is-active-nav href="#feed">Feed</a></li>
<li><a is-active-nav href="#test">Test</a></li>
<li><a is-active-nav href="#feedback">Feedback</a></li>
</ul>
And my JS like:
var app = angular.module('app', ['ngRoute', 'ngAnimate']);
app.directive('isActiveNav', [ '$location', function($location) {
return {
restrict: 'A',
link: function(scope, element) {
scope.location = $location;
scope.$watch('location.path()', function(currentPath) {
if('/#' + currentPath === element[0].attributes['href'].nodeValue) {
element.parent().addClass('active');
} else {
element.parent().removeClass('active');
}
});
}
};
}]);
Looked at this: (http://blog.bastisteiner.com/entry/add-active-to-menu-with-angularjs)
But I can’t get it to work, the link doesn’t get the class. Can anyone see why?