最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set different body background for different pages when using AngularJS template and ui-router - Stack Overflow

programmeradmin0浏览0评论

I have two pages and I'm using ui.router to jump around different pages. One is a login page and after login, the page will jump to a home page.I want to set the a body background-color for login page but set another body background-color for another. If I just set color for the template div, the color won't be applied to the body background, but if set color directly to the background, then two pages have the same color. What can I do?

HTML:

 <body>
    <div ui-view="header"></div>
    <div ui-view="content"></div>
    <!-- build:js scripts/main.js -->
</body>

JS:

angular.module('app',[....]).config(function ($stateProvider,$urlRouterProvider) {
    $stateProvider
        .state('login',{
            url:'/',
            views:{
                'content':{
                    templateUrl:"views/login.html",
                    controller:"LoginController"
                }
            }
        })
        .state('home',{
            url:'/home',
            views:{
                'header':{
                    templateUrl : "views/header.html",
                    controller: 'HeaderController'                                             
                },
                'content':{
                    templateUrl: "views/homePage.html",
                    controller : 'HomeController'
                }
            }
        })

I have two pages and I'm using ui.router to jump around different pages. One is a login page and after login, the page will jump to a home page.I want to set the a body background-color for login page but set another body background-color for another. If I just set color for the template div, the color won't be applied to the body background, but if set color directly to the background, then two pages have the same color. What can I do?

HTML:

 <body>
    <div ui-view="header"></div>
    <div ui-view="content"></div>
    <!-- build:js scripts/main.js -->
</body>

JS:

angular.module('app',[....]).config(function ($stateProvider,$urlRouterProvider) {
    $stateProvider
        .state('login',{
            url:'/',
            views:{
                'content':{
                    templateUrl:"views/login.html",
                    controller:"LoginController"
                }
            }
        })
        .state('home',{
            url:'/home',
            views:{
                'header':{
                    templateUrl : "views/header.html",
                    controller: 'HeaderController'                                             
                },
                'content':{
                    templateUrl: "views/homePage.html",
                    controller : 'HomeController'
                }
            }
        })
Share Improve this question edited May 23, 2016 at 6:47 Shashank Agrawal 25.8k11 gold badges96 silver badges125 bronze badges asked May 23, 2016 at 6:40 Wayne LiWayne Li 4211 gold badge6 silver badges17 bronze badges 1
  • Add background-color to root tag (may be ion-view tag) of your homePage template using bg-color inline attribute. or from CSS. – Harish Kommuri Commented May 23, 2016 at 7:23
Add a ment  | 

3 Answers 3

Reset to default 4

In a global controller (which is applied to either <html> or <body> tag), register an event:

myApp.controller('GlobalCtrl', function($scope) {
    // Event listener for state change.
    $scope.$on('$stateChangeStart', function(event, toState, toParams) {
        $scope.bodyClass = toState.name + '-page';
    });
});

Now, in your HTML:

<body class="{{bodyClass}} foo bar" ng-controller="GlobalCtrl">
   <!-- your content -->
</body>

Now, in your CSS:

body.login-page {
    background: green;
}

body.home-page {
    background: red;
}

For index page:

<body ng-class="{'home-page': $state.includes('home'), 'login-page' : $state.includes('login')}">
    <div ui-view="header"></div>
    <div ui-view="content"></div>
    <!-- build:js scripts/main.js -->
</body>

For app.run.js, add this code first.

$rootScope.$state = $state;

CSS:

body.home-page {
  background: yellow;
}

body.login-page {
  background: blue;
}

While using angular-router, from your main app you will get an event $statechangeSuccess, when the state gets changed you will get to know on which state you are and probably change the background using ng-style or ng-class on body:

HTML

<body ng-class='{$scope.state==="home":class1,$scope.state==="login":class2}'>

JS

    app.controller('appCtrl', function($scope) {
      $scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
        console.log(event, toState, toParams, fromState, fromParams);
$scope.state = toState;//do the checking
      });
    });

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论