I change the class on my <body> element, depending on which $state my website is currently showing.
I say <body class="{{specialClass}}">, where specialClass is a variable of $rootScope. This variable is set in run block of my main app module:
angular.module("main", [dependencies])
.run(function($rootScope) {
$rootScope.$on('$stateChangeSuccess', function(event, currentState){
switch (currentState.name) {
case "about":
console.log("I'm on landing page, set specialClass in $rootScope");
$rootScope.specialClass = 'landing-page';
break;
default:
$rootScope.specialClass = '';
break;
}
})
});
For some reason, my template doesn't see specialClass variable, so the class attribute is empty. At the same time, "I'm on landing page, set specialClass in $rootScope" shows up.
What's wrong?
My full index.html template:
<html>
<head>
<base href="/">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My project</title>
</head>
<body ng-app="main" class="{{specialClass}}">
<ui-view>
This is my application.
</ui-view>
</body>
</html>
What's even more crazy is when I serve frontend in development with Webpack dev server, it works ok, the class is switched. But when I serve this in production with Django, it is not. Console output is present in both cases.
ngAppdirective must contain the body tag.ngAppdirective is applied to the body tag. It is in fact:<body ng-app="main" class="{{specialClass}}">.