Angularjs 在主路由上添加css类

Angularjs 在主路由上添加css类,angularjs,Angularjs,在标题中,我想在主页的标题中添加一个css类“homeClass” 这是我的配置和attr指令 app.config(function($routeProvider){ $routeProvider .when("/", { templateUrl: "/partial/home.html", isMain:true }); }); app.directive('animClass',function($route){ return { link

在标题中,我想在主页的标题中添加一个css类“homeClass”

这是我的配置和attr指令

app.config(function($routeProvider){
$routeProvider .when("/", {
        templateUrl: "/partial/home.html",
        isMain:true
    });
});

app.directive('animClass',function($route){
return {
    link: function(scope, elm, attrs){
        if($route.current.isMain)
            elm.addClass('homeclass');
    }
}
});
index.html

<header class="intro-header" anim-class>
...
</header>

...

我做错了什么?我很确定isMain在指令中是不可访问的。

您可以使用ng类,因为您的指令将在路由更改之前触发:

app.directive('animClass',function($route){
    return {
        link: function(scope, elm, attrs){
            scope.$route = $route;
        }
    }
});
然后,对于您的指令,您可以连接到:

<header class="intro-header" anim-class ng-class="{
    'homeClass': $route.current.isMain
}">
...
</header>

...

我不确定你的
$scopes
/partials是如何连接起来的,但这将是一般的想法。

它可以工作,谢谢。我不完全确定原因,但它是有效的:)
scope.$route=$route