Last active
March 21, 2017 02:24
-
-
Save martianyi/043f7f857d7e9ac587d6 to your computer and use it in GitHub Desktop.
angularjs classRoute directive, usage <body class-route=" ">
This file contains hidden or 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
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