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

I have two pages and I'm using ui.router to jump around different pages. One is a login page and a

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
      });
    });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745488895a4629896.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信