I am using Angular Routing on my page and I also want to highlight menu item corresponded to current content.
I tried to use ngClass and controller like in this post but it has no effect
Here's the code:
index.html
<body ng-app="sApp">
<div ng-controller="NavCtrl" class="nav">
<ul>
<li ng-class="{ active-btn: isActive('/')}">
<a href="#" class="menu-btn">HOME</a>
</li>
<li ng-class="{ active-btn: isActive('/1')}">
<a href="#/1" class="menu-btn">PAGE1</a>
</li>
<li ng-class="{ active-btn: isActive('/2')}">
<a href="#/2" class="menu-btn">PAGE2</a>
</li>
</ul>
</div>
<div class="content" ng-view=""></div>
style.css
.nav {
padding: 1rem;
text-align: center;
background-color: #ffa500;
}
.nav ul li {
display: inline;
font-weight: 700;
}
.menu-btn {
padding: 1rem;
}
.menu-btn:hover {
background-color: #ee82ee;
}
.active-btn {
background-color: #00f;
}
script.js
'use strict';
var sApp;
sApp = angular.module('sApp', ['ngRoute']);
sApp.config(function($routeProvider) {
return $routeProvider
.when('/', {templateUrl: 'h.html'})
.when('/1', {templateUrl: '1.html'})
.when('/2', {templateUrl: '2.html'})
.otherwise({redirectTo: '/'});
});
sApp.controller('NavCtrl', function($scope, $location) {
$scope.isActive = function(viewLocation) {
return viewLocation === $location.path();
};
});
Here is the plunker:
It looks like that menu items can't change the classes to active-btn. Can you help me please?