You must have your ng-app attached in the <html> element in order to have any connection between angular and view. Since you want something to change to the current route of your application, why not use these routes as a reference for your configuration, for example. $routeProvider configurations. Attach your entire configuration, including configuration from classes to styles, or any other configuration in the route object. Then you can create a directive that will listen for route changes using $routeChangeSuccess , and then get the current route and other properties using the $route object defined as the second parameter of the $routeChangeSuccess , after you have these properties, you can do whatever you want with it, eg add a class to this element of the directive.
Demo
Javascript
Configuration
.config(function ($routeProvider) { $routeProvider .when('/dashboard', { templateUrl: 'dashboard.html', 'class': 'bg-dashboard' }) .when('/login', { templateUrl: 'login.html', 'class': 'bg-login' }) .otherwise({redirectTo: '/login'}); });
Directive
.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); } } }); }; });
HTML
<html ng-app="myApp" class-route>...</html>
source share