Skip to content

Instantly share code, notes, and snippets.

@ecowden
Last active December 12, 2015 09:58
Show Gist options
  • Save ecowden/4755050 to your computer and use it in GitHub Desktop.
Save ecowden/4755050 to your computer and use it in GitHub Desktop.
In Angular, match the current route to set the active element in a navigation bar, etc.
/*
* In your controller, create a function that compares the current location to a regular expression.
*/
$scope.routeMatches = function (fragment) {
var matcher = new RegExp('^' + fragment + '$', ['i']);
return matcher.test($location.path());
};
/*
* In your partial, use the above function to assign an appropriate 'active' CSS class.
* You can also drive another directive like ng-show, etc.
*/
<li ng-class="{active: routeMatches('/templates.*')}">
<a href="#/templates/manage"><i class="icon icon-tasks bigIcon"></i> Templates</a>
</li>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment