Skip to content

Instantly share code, notes, and snippets.

@martianyi
Last active March 21, 2017 02:24
Show Gist options
  • Save martianyi/043f7f857d7e9ac587d6 to your computer and use it in GitHub Desktop.
Save martianyi/043f7f857d7e9ac587d6 to your computer and use it in GitHub Desktop.
angularjs classRoute directive, usage <body class-route=" ">
var classRoute = angular.module('directives.classRoute',[]);
classRoute.directive('classRoute', function($rootScope, $route) {
return function(scope, elem, attr) {
var previous = '';
$rootScope.$on('$routeChangeSuccess', function(event, currentRoute) {
var route = currentRoute.$$route;
if(route) {
var cls = route['class'];
if(previous) {
attr.$removeClass(previous);
}
if(cls) {
previous = cls;
attr.$addClass(cls);
}
}
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment