Highlight current menu using AngularJS

2019-08-17 18:43发布

问题:

I am using the following code:

<li class="sidebar-list-item"><a ng-class="getClass('/dashboardHome')" href="/dashboardHome" class="sidebar-link text-muted"><i class="o-home-1 mr-3 text-gray"></i><span>Home</span></a></li>


$scope.getClass = function (path) {
        console.log("Path: "+path);
        return ($location.path().substr(0, path.length) === path) ? 'active' : '';
      }

On console I am getting : Path: /dashboardHome

But not its not working.

回答1:

Do like this if you use ui-sref.

ui-sref-active="active" ui-sref="home.dashboard.dashboardHome"

You do not need to use ng-class and getClass function.

Update

ui-sref-active-eq:

The ui-sref-active-eq directive applies the CSS class when the ui-sref/ui-state's target state is directly active (not when child states are active). This is an "exact match" which uses StateService.is. -ui-router.github.io

If you do not want to activate the parent route when a child route is active, use ui-sref-active-eq="active" for the parent route.