【发布时间】:2014-10-28 16:30:50
【问题描述】:
我的路线文件
app.config(["$routeProvider",function($route)
{
$route.when("/",{
templateUrl : "partials/index.html",
controller : "AppCtrl"
}).when("/contact",{
templateUrl:"partials/contact.html",
controller:"ContactCtrl"
}).
when("/about-us",{
templateUrl:"partials/about.html",
controller:"AboutCtrl"
}).
when("/gallery",{
templateUrl:"partials/gallery.html",
controller:"GalleryCtrl"
}).
otherwise({
redirectTo:"/"
});
}]);
在我的 header.html 部分中,我使用的是 HeaderCtrl 控制器
app.controller("HeaderCtrl",["$scope","$location",function($scope,$location)
{
$scope.location=$location.path().replace("/","");
}]);
我的 header.html
<ul ng-controller="HeaderCtrl">
<li ng-class="{active:location===''}"><a href="#">home</a></li>
<li ng-class="{active:location==='about-us'}"><a href="#/about-us">about us</a></li>
<li ng-class="{active:location==='gallery'}"><a href="#/gallery">gallery</a></li>
<li ng-class="{active:location==='contact'}"><a href="#/contact">contact</a></li>
</ul>
当页面重新加载(实际刷新)时它可以工作,即活动类应用于相应的 li 但是当我更改路线时(通过单击菜单项)它不起作用
【问题讨论】:
-
那是因为当路由改变时你没有改变控制器中的
$scope.location。 -
是的,我认为是这样,我该如何克服呢?