Skip to content

Instantly share code, notes, and snippets.

@chrisenytc
Created October 3, 2014 13:15
Show Gist options
  • Save chrisenytc/8c28a52cd6f3f3bb167f to your computer and use it in GitHub Desktop.
Save chrisenytc/8c28a52cd6f3f3bb167f to your computer and use it in GitHub Desktop.
Add class active to a menu with AngularJS directive
'use strict';
angular.module('App').directive('isActive', ['$location', function($location) {
return {
restrict: 'A',
link: function(scope, element) {
scope.location = $location;
scope.$watch('location.path()', function(currentPath) {
var formatedPath = '';
if(currentPath === '/') {
formatedPath = '/#';
} else {
formatedPath = '/#' + currentPath;
}
if (formatedPath === element[0].children[0].attributes['href'].value) {
element.addClass('active');
} else {
element.removeClass('active');
}
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment