角JS分配类视视图body标签显示? [英] Angular JS assign class to body tag depending on view displayed?

查看:114
本文介绍了角JS分配类视视图body标签显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an angular application similar to this:

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

     ...stuff...

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

     ....

和视图是由路由设置:

 .when('/maps', {
            templateUrl: 'views/maps.html',
            controller: 'MapsCtrl'
        })

它工作正常。我想知道的是如何从当前位置获得一个类,使用上面的例子中,指定一个类映射到body标签的特定页面的CSS。

It works fine. What I'd like to know is how I can get a class from the current location, using the above example, assign a class "maps" to the body tag for page-specific CSS.

推荐答案

我会在设置一个MainController听众是这样的:

I would setup a listener in MainController like this:

$scope.$on('$routeChangeSuccess', function(newVal, oldVal) {
    if (oldVal !== newVal) {
        $scope.routeClassName = $route.current.className;
    }
});

和模板:

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

然后,你可以配置这样的路线:

Then you can configure routes like this:

$routeProvider
.when('/maps', {
    controller: 'mapsController',
    template: 'maps',
    className: 'maps'   // <--- class name
})
.when('/about', {
    controller: 'aboutController',
    template: 'about',
    className: 'about'  // <--- class name
})

这篇关于角JS分配类视视图body标签显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆