使用AngularJS突出显示当前菜单

问题描述:

我正在使用以下代码:

<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' : '';
      }

在控制台上,我得到:路径:/dashboardHome

On console I am getting : Path: /dashboardHome

但它不起作用.

如果使用ui-sref,请执行以下操作.

Do like this if you use ui-sref.

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

您不需要使用ng-class和getClass函数.

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

ui-sref-active-eq:

ui-sref-active-eq:

ui-sref-active-eq指令在以下情况下应用CSS类: ui-sref/ui-state的目标状态直接处于活动状态(不是在子状态时 状态有效).这是完全匹配",它使用 StateService.is. - ui-router.github.io

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

如果您不想在子路由处于活动状态时激活parent路由,请对父路由使用ui-sref-active-eq="active".

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.