I have three buttons labeled About Us, Contacts, and Services. Whenever I click the button, the hash value will be changed, and I used the concept of routing to go to different pages.
Now I want to use the function $location.hash()in AngularJS and change the color of the buttons whenever the hash value changes.
See the code I wrote below:
<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>
Here is the angular js code I wrote:
app.controller('mainController', ['$scope', '$location', function($scope,$location){
var location = $location.hash();
}]);
Here's the css:
.active{
background-color: red;
color : white;
}
a{
padding:10px;
background-color: white;
color: black;
border: 1px solid black;
}
I don’t know what is wrong with the code, but I can’t get the color, the class is not added to the tag when clicked.
source
share