【问题标题】:How to set a class on a containing div如何在包含的 div 上设置一个类
【发布时间】:2014-12-23 18:52:35
【问题描述】:

您好,感谢您查看我的问题。我是 angularjs 新手,因此给我尽可能多的指导。

我有 2 页。 (1) - singlepage.php (这个真的是其他所有页面的主要容器 (2) - login.html(这个实际上是一个包含用户名、密码等文本框的片段

<!-- fragment from singlepage.php --> <body ng-controller="AppController" class="{{bodyClass}}"> <div id="view" ng-view></div> </body>

    $routeProvider.when('/login', {
        templateUrl: 'templates/login.html',
        bodyclass: 'login',
        controller: 'LoginController'
    });

    app.controller("LoginController", function($scope, $location, AuthenticationService, $route) {
    $scope.bodyclass = 'login';

    $scope.credentials = { email: "", password: "" };
    $scope.login = function() {
    AuthenticationService.login($scope.credentials).success(function() {
         $location.path('/home');
      });
    };
});

我需要在登录页面加载时将 body 元素上的类更改为“loginbox”。

再次感谢

【问题讨论】:

    标签: angularjs angularjs-directive


    【解决方案1】:

    Documentation for ng-class

    ng-class="{bodyClass: true}"

    【讨论】:

      【解决方案2】:

      有 2 篇参考资料可以帮助您走上这条道路:

      1. ngClass 指令文档 - https://docs.angularjs.org/api/ng/directive/ngClass#!
      2. 此帖:What is the best way to conditionally apply a class?

      【讨论】:

        【解决方案3】:

        您可以使用 $rootScope 设置模板元素的视图模型。 例如:

        app.controller("LoginController", function($rootScope, $scope, $location, AuthenticationService, $route) {
          $rootScope.bodyClass = "loginbox";
          ...
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-04
          • 1970-01-01
          • 2022-06-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多