我有三个按钮,标记为关于我们,联系我们 and 服务。每当我点击一个按钮时,散列值将被改变,并且我已经使用路由概念去到不同的页面。
现在,我想在AngularJS中使用函数$location.hash()
,并在哈希值更改时更改按钮的颜色。
见我写了下面的代码:
<div ng-controller="mainController">
<a href = "#/aboutus" ng-class="{'active': location == '/aboutus'}">About us</a>
<a href = "#/contactus" ng-class="{'active': location == '/contactus'}">Contact us</a>
<a href = "#/services" ng-class="{'active': location == '/services'}">Services</a>
</div>
这里的角的js代码,我已经写了:
app.controller('mainController', ['$scope', '$location', function($scope,$location){
var location = $location.hash();
}]);
这里的CSS:
.active{
background-color: red;
color : white;
}
a{
padding:10px;
background-color: white;
color: black;
border: 1px solid black;
}
我不不知道代码有什么问题,但我无法获得颜色,单击时不会将类添加到标记中。
该死的,我真的忘了包括$范围,谢谢你。我会尝试:) – Harish
它仍然无法正常工作,要检查它是否正常工作我使用ngclick和一个函数来执行每当按钮被点击时,我已经在这个函数中记录$ scope.location。 $ scope.location的值在记录时是空的@Maarteen – Harish
稍后我会看到我是否可以为您提供一个工作示例。 –