Skip to content

Instantly share code, notes, and snippets.

@naxmefy
Created November 22, 2013 20:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save naxmefy/7606111 to your computer and use it in GitHub Desktop.
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
.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