Javascript 是否根据显示的视图将类指定给主体标记?

Javascript 是否根据显示的视图将类指定给主体标记?,javascript,css,html,angularjs,Javascript,Css,Html,Angularjs,我有一个类似的角度应用程序: <html> <body ng-app="my-app" ngcloak ng-controller="MainCtrl"> ...stuff... <!-- Add your site or application content here --> <div ng-view=""></div> .... 它很好用。我想知道的是如何从当前位置获取类,

我有一个类似的角度应用程序:

<html>
   <body ng-app="my-app" ngcloak ng-controller="MainCtrl">

     ...stuff...

        <!-- Add your site or application content here -->
    <div ng-view=""></div>

     ....

它很好用。我想知道的是如何从当前位置获取类,使用上面的示例,为特定于页面的CSS的body标记分配一个类“maps”。

您可以使用$location服务找出当前位置,然后使用ng类将其分配给body标记。e、 g

JS

HTML


我会在MainController中设置一个侦听器,如下所示:

$scope.$on('$routeChangeSuccess', function(newVal, oldVal) {
    if (oldVal !== newVal) {
        $scope.routeClassName = $route.current.className;
    }
});
$routeProvider
.when('/maps', {
    controller: 'mapsController',
    template: 'maps',
    className: 'maps'   // <--- class name
})
.when('/about', {
    controller: 'aboutController',
    template: 'about',
    className: 'about'  // <--- class name
})
在模板中:

<body ng-controller="mainController" ng-class="routeClassName">

然后,您可以像这样配置路由:

$scope.$on('$routeChangeSuccess', function(newVal, oldVal) {
    if (oldVal !== newVal) {
        $scope.routeClassName = $route.current.className;
    }
});
$routeProvider
.when('/maps', {
    controller: 'mapsController',
    template: 'maps',
    className: 'maps'   // <--- class name
})
.when('/about', {
    controller: 'aboutController',
    template: 'about',
    className: 'about'  // <--- class name
})
$routeProvider
.when(“/maps”{
控制器:“mapsController”,
模板:“地图”,
类名:“地图”//
$routeProvider
.when('/maps', {
    controller: 'mapsController',
    template: 'maps',
    className: 'maps'   // <--- class name
})
.when('/about', {
    controller: 'aboutController',
    template: 'about',
    className: 'about'  // <--- class name
})