If I have a navbar in bootstrap with the items
Home | About | Contact
How do I set the active class for each menu item when they are active? That is, how can I set class="active"
when the angular route is at
#/
for home#/about
for the about page#/contact
for the contact pageTo accomplish this task, you can use ng-controller(NavigationController) to set the bootstrap navbar active class with AngularJS. To run a single controller outside ng-view. You can set class= “active” when the angular route is clicked.
To set the active class to the navigation menu dynamically by scrolling or clicking on the navigation links, the active class is to be set on each section depending on the position of the webpage. To add methods and variables, JavaScript is used.
The active class is applied to the navigation element the user is currently viewing. In the case of your given code, the user is viewing the the profile. It will serve as a guide or reminder to where in the website the visitor is in.
To create a collapsible navigation bar, use a button with class="navbar-toggler", data-toggle="collapse" and data-target="#thetarget" . Then wrap the navbar content (links, etc) inside a div element with class="collapse navbar-collapse" , followed by an id that matches the data-target of the button: "thetarget".
A very elegant way is to use ng-controller to run a single controller outside of the ng-view:
<div class="collapse navbar-collapse" ng-controller="HeaderController"> <ul class="nav navbar-nav"> <li ng-class="{ active: isActive('/')}"><a href="/">Home</a></li> <li ng-class="{ active: isActive('/dogs')}"><a href="/dogs">Dogs</a></li> <li ng-class="{ active: isActive('/cats')}"><a href="/cats">Cats</a></li> </ul> </div> <div ng-view></div>
and include in controllers.js:
function HeaderController($scope, $location) { $scope.isActive = function (viewLocation) { return viewLocation === $location.path(); }; }
I just wrote a directive to handle this, so you can simply add the attribute bs-active-link
to the parent <ul>
element, and any time the route changed, it will find the matching link, and add the active
class to the corresponding <li>
.
You can see it in action here: http://jsfiddle.net/8mcedv3b/
Example HTML:
<ul class="nav navbar-nav" bs-active-link> <li><a href="/home">Home</a></li> <li><a href="/contact">Contact</a></li> </ul>
Javascript:
angular.module('appName') .directive('bsActiveLink', ['$location', function ($location) { return { restrict: 'A', //use as attribute replace: false, link: function (scope, elem) { //after the route has changed scope.$on("$routeChangeSuccess", function () { var hrefs = ['/#' + $location.path(), '#' + $location.path(), //html5: false $location.path()]; //html5: true angular.forEach(elem.find('a'), function (a) { a = angular.element(a); if (-1 !== hrefs.indexOf(a.attr('href'))) { a.parent().addClass('active'); } else { a.parent().removeClass('active'); }; }); }); } } }]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With