Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set bootstrap navbar active class with Angular JS?

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

  1. #/ for home
  2. #/about for the about page
  3. #/contact for the contact page
like image 264
user1876508 Avatar asked Apr 24 '13 18:04

user1876508


People also ask

How set navbar active class in Bootstrap?

To 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.

How do I set active class nav menu?

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.

What is class active Bootstrap?

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.

How do I toggle navbar in Bootstrap?

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".


2 Answers

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();     }; } 
like image 137
myl Avatar answered Sep 25 '22 10:09

myl


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');                    };             });              });     } } }]); 
like image 45
dave Avatar answered Sep 23 '22 10:09

dave