Skip to content

Instantly share code, notes, and snippets.

@thm-design
Last active January 2, 2016 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thm-design/8280929 to your computer and use it in GitHub Desktop.
Save thm-design/8280929 to your computer and use it in GitHub Desktop.
Angular snippet : active navigation
app.directive("activeNav",function($location) {
return {
link: function postLink(scope, element, attrs) {
scope.$on("$routeChangeSuccess", function (event, current, previous) {
// this var grabs the tab-level off the attribute, or defaults to 1
var pathLevel = attrs.activeNav || 1,
// this var finds what the path is at the level specified
pathToCheck = $location.path().split('/')[pathLevel],
// this var finds grabs the same level of the href attribute
tabLink = attrs.href.split('#')[pathLevel];
// now compare the two:
if (pathToCheck === tabLink) {
element.addClass("active");
}
else {
element.removeClass("active");
}
});
}
};
});
// add directive name to each nav element (must be <a> tag for href comparison)
<a class="icon icon-lab" href="#portfolio" active-nav="1">Portfolio</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment