0

I have one issue while using Angular.js ui-router.I added active class for highlight the menu but its not working.I am explaining my code below.

<ul class="nav navbar-nav">

           <li ui-sref-active="active" ><a ui-sref=".profile">College Profile</a></li>
<li ng-class="{active: $state.includes('dashboard.deptmanagement')}"><a ui-sref="dashboard.deptmanagement.stream" >Department Management</a></li>
<li  ui-sref-active="active">
            <li ui-sref-active="active"><a ui-sref="dashboard.user.usermanagement">User Management</a></li>
             <li ui-sref-active="active"><a ui-sref="dashboard.plan.contime">Plan Management</a></li>

          </ul> 

My problem is in Department Management menu from the above code.As this menu has some sub menus if i am adding here ui-sref-active="active" the issue is coming when the sub menu are selecting this menu can not get any active class.The sub menu of this parent menu is given below.

deptmanagement.html:

    <div>
     <tabset>
    <tab ui-sref=".stream" ui-sref-active="active">
      <tab-heading>Add Stream</tab-heading>
    </tab>
    <tab ui-sref=".dept" ui-sref-active="active">
      <tab-heading>Add Department</tab-heading>
    </tab>
    <tab ui-sref=".course" ui-sref-active="active">
      <tab-heading>Add Course</tab-heading>
    </tab>
    <tab ui-sref=".sub" ui-sref-active="active">
      <tab-heading>Add Subject</tab-heading>
    </tab>
  </tabset>
  <div ui-view></div>
</div>

loginroute.js

var Admin=angular.module('Channabasavashwara',['ui.router', '720kb.datepicker','ngMessages','ngCapsLock','ui.bootstrap']);
Admin.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/');
    $stateProvider
     .state('/', {
            url: '/',
            templateUrl: 'dashboardview/login.html',
            controller: 'loginController'
        })
        .state('dashboard', {
            url: '/dashboard',
            templateUrl: 'dashboardview/dashboard.html',
            controller: 'dashboardController'
        })
        .state('dashboard.profile', {
        url: '/profile',
        templateUrl: 'dashboardview/profile.html',
        controller: 'profileController'
    })
    .state('dashboard.deptmanagement', {
        url: '/deptmanagement',
        templateUrl: 'dashboardview/deptmanagement.html',
        controller: 'deptmanagementController'
    })
    .state('dashboard.deptmanagement.stream', {
        url: '/stream',
        templateUrl: 'dashboardview/stream.html',
        controller: 'streamController'
    })
    .state('dashboard.deptmanagement.course', {
        url: '/course',
        templateUrl: 'dashboardview/course.html',
        controller: 'resourcecourseController'
    })
    .state('dashboard.deptmanagement.sub', {
        url: '/subject',
        templateUrl: 'dashboardview/subject.html',
        controller: 'deptsubjectController'
    })
    .state('dashboard.deptmanagement.dept', {
        url: '/dept',
        templateUrl: 'dashboardview/dept.html',
        controller: 'deptController'
    })
})

Here my requirement is when any of above sub-menu will select the main menu remain highlight.Please help me to resolve this issue.

6
  • Can you please include your routing js Commented Nov 10, 2015 at 4:20
  • Try adding quotes around the class name <li ng-class="{'active': $state.includes('dashboard.deptmanagement')}">. It looks like you're using bootstrap so you may also like to keep the menu open for sub menu items so use <li ng-class="{'active open': $state.includes('dashboard.deptmanagement')}"> Commented Nov 10, 2015 at 4:29
  • @Tristan : No using your way also the menu is not getting highlight. Commented Nov 10, 2015 at 4:34
  • $state should be available in view. ie $scope.$state = $state; Commented Nov 10, 2015 at 4:40
  • Getting confused..Where it should be added. Commented Nov 10, 2015 at 4:41

1 Answer 1

1

To use ng-class="{'active':$state.includes('route1')}" for navigation $state needs to be available to the view, ie defined on the $scope in the controller.

$scope.$state = $state;

You can define $rootScope.$state = $state; so that it is available on $rootScope throughout your app although this may be considered poor practice.

If your controller is mainCtrl then:

Admin.controller('mainCtrl', ['$scope', '$state', function($scope, $state) {
   $scope.$state = $state;
}]);

Or if you want to define $state on $rootScope you can use .run()

Admin.run(function($rootScope, $state) {
   $rootScope.$state = $state;
})

Because the submenu is a tabset you can compare the current state name for each tab to set the active tab

<div> 
<tabset> 
<tab ui-sref="dashboard.deptmanagement.stream" active="$state.current.name == 'dashboard.deptmanagement.stream'"> 
<tab-heading>Add Stream</tab-heading> 
</tab> 
<tab ui-sref="dashboard.deptmanagement.dept" active="$state.current.name == 'dashboard.deptmanagement.dept'"> 
<tab-heading>Add Department</tab-heading> 
</tab> 
<tab ui-sref="dashboard.deptmanagement.course" active="$state.current.name == 'dashboard.deptmanagement.course'"> 
<tab-heading>Add Course</tab-heading> 
</tab> 
<tab ui-sref="dashboard.deptmanagement.sub" active="$state.current.name == 'dashboard.deptmanagement.sub'"> 
<tab-heading>Add Subject</tab-heading> 
</tab> 
</tabset> 
<div ui-view></div> 
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

Ok,I added as per you and it started work.But one issue is there.When i am refreshing any sub menu page rather than 1st sub menu,i am getting two sub menu including 1st sub menu is highlighting at a time.
Can you post the code? Two sub menu items must be returning true for the route.
@ Tristan:Please check my post.'deptmanagement.html' is the sub menu file code.For default sub menu its ok.But when i am refreshing page of any other submenu.Two submenu is highlighting including 1st sub-menu at a time.