Created
November 22, 2013 20:16
-
-
Save naxmefy/7606111 to your computer and use it in GitHub Desktop.
Angular Directive for Menu State to Set Class Attributes or else to identify if it is active. Works with mainnavs and subitems. only add directive "when-active" to menuitems
This file contains 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
.directive('whenActive', | |
[ | |
'$location', | |
($location)-> | |
scope: true, | |
link: (scope, element, attr)-> | |
scope.$on '$routeChangeSuccess', | |
() -> | |
loc = "#"+$location.path() | |
href = element.attr('href') | |
state = href.indexOf(loc) | |
substate = -1 | |
if href.length > 3 | |
substate = loc.indexOf(href) | |
if loc.length is 2 | |
state = -1 | |
#console.log "Is Loc: "+loc+" in Href: "+href+" = "+state+" and Substate = "+substate | |
if state isnt -1 or substate isnt -1 | |
element.addClass 'selected' | |
element.parent().addClass 'current-menu-item' | |
else if href is '#' and loc is '#/' | |
element.addClass 'selected' | |
element.parent().addClass 'current-menu-item' | |
else | |
element.removeClass 'selected' | |
element.parent().removeClass 'current-menu-item' | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment