Created
October 3, 2014 13:15
-
-
Save chrisenytc/8c28a52cd6f3f3bb167f to your computer and use it in GitHub Desktop.
Add class active to a menu with AngularJS directive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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