为什么 ng-class 未根据 ui 路由器的当前状态应用?

Why ng-class is not applied as per the current state of ui router?

我的应用程序有两个不同的视图(page1 和 page2),已使用 ui-routerstateProvider 配置。我想根据我正在查看的页面在正文上应用 css。如果我正在查看第 1 页,我想将 class1 应用到页面正文,并将 class2 应用到第 2 页。

为了实现这一点,我使用了 angularjsng-class 指令。 ng-class 指令的值是根据 ui-router.

的当前状态设置的

这是它的示例代码,问题是 ng-class 中的表达式没有被计算,所以我的 css 没有被应用。

谁能指出我做错了什么?

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.4.2/angular-ui-router.min.js"></script>

    <style media="screen">

    h2 {
      margin: 5px 5px 5px 5px;
    }
    body {
      display: flex;
      flex-direction: column;
    }
    .class1 {
      align-items: flex-start;
    }
    .class2 {
      align-items: center;
    }
    </style>
  </head>

  <body ng-app="graceApp" ng-class="$state.current.data.bodyClass">

    <div ui-view style="display: flex"></div>

    <script>
    angular
      .module('graceApp', [
        'ui.router'
      ])
      .config(function ($stateProvider, $urlRouterProvider) {
        $stateProvider
          .state('page1', {
            url: '/page1',
            template: '<h2>This is page1</h2> <h2>Item1</h2> <h2>Item2</h2>',
            data: {
              bodyClass: 'class1'
            }
          })
          .state('page2', {
            url: '/page2',
            template: '<h2>This is page2</h2> <h2>Item1</h2> <h2>Item2</h2>',
            data: {
              bodyClass: 'class2'
            }
          });
        $urlRouterProvider.otherwise('/page1');
      });
    </script>
</body>
</html>

我不确定 $state 服务是否可以像这样在 HTML 中访问。您是否考虑过在您的控制器中创建一些功能 return $state.current.data.bodyClass 然后,您 link 该功能进入 ng-class

function getClass() {
    return $state.current.data.bodyClass;
}

HTML:

<body ng-app="graceApp" ng-class="getClass()">

如果您使用的是 controllerAs 模式,请确保创建此函数 public 并在 HTML 中使用控制器别名。

$state 服务在 HTML 中不可用。您只能在 html 中访问作用域中设置的变量,因此快速修复是在 rootScope 中设置状态,然后您可以在 HTML.

中访问它
$rootScope.$state = $state;

你不能在html中直接访问$state,所以你需要在控制器中定义另一个作用域变量,比如

<!doctype html>
<html ng-app="graceApp">

<head>
    <meta charset="utf-8">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.4.2/angular-ui-router.min.js"></script>
    <style media="screen">
    .class1 {
        background-color: red;
    }

    .class2 {
        background-color: green;
    }
    </style>
</head>
<body ng-controller="mainController" ng-class="bodyClass">
    <div ui-view></div>
    <script>
    angular.module('graceApp', ['ui.router']).config(function($stateProvider, $urlRouterProvider) {
        $stateProvider
            .state('page1', {
                url: '/page1',
                template: '<h2>This is page1</h2> <h2>Item1</h2> <h2>Item2</h2>',
                data: {
                    bodyClass: 'class1'
                }
            })
            .state('page2', {
                url: '/page2',
                template: '<h2>This is page2</h2> <h2>Item1</h2> <h2>Item2</h2>',
                data: {
                    bodyClass: 'class2'
                }
            });
        $urlRouterProvider.otherwise('/page1');
    });

    angular.module("graceApp").controller("mainController", function($rootScope, $scope) {
        $rootScope.$on("$stateChangeSuccess", function(event, current, params) {
            console.log(current.data.bodyClass);
            $scope.bodyClass = current.data.bodyClass;
        });
    });
    </script>
</body>
</html>