app .directive('activeLink', function($location) { return { restrict: 'A', link: function($scope, $element, attrs) { var classname = attrs.activeLink; var path = $element.children('a[href]').attr('href'); $scope.location = $location; $scope.$watch('location.path()', function(newPath) { if ((path === '/' && path === newPath) || (newPath.indexOf(path) === 0 && path !== '/')) { $element.addClass(classname); } else { $element.removeClass(classname); } }); } }; });