简体   繁体   English

角线菜单激活

[英]Angular route menu active

I try to explain my problem in advance sorry for my bad english. 我想提前解释一下我的问题,对不起我的英语不好。

I have written code with angular and angular route My navbar looks like this in html 我已经用角度和角度路线编写了代码,我的导航栏在html中看起来像这样

<ul nav-menu>
    <li><a href="">Index</a></li>
    /*others*/
</ul>

And route params 和路由参数

    $routeProvider
        .when('/', {
            templateUrl: 'views/main.html',
            controller: 'mainCtrl' 
        })
        .when('/skills', {
            // others
        })

Everything works perfectly but my questin is: 一切正常,但我的追求是:

How can I set some css to my li element when route has been loaded or active? 路由已加载或处于活动状态时,如何为li元素设置一些CSS?

You have to deal with more than topic here. 您在这里不仅要处理主题

  1. First, have a look at ngClass . 首先,看看ngClass There you can put Angular logic inside the class, so it's changing with the scope values or function returns. 在那里,您可以将Angular逻辑放入类中,因此它随作用域值或函数返回而变化。
  2. Therefore you have to deal with passing scopes and data through controllers, or depending how your app is structured. 因此,您必须处理通过控制器传递的范围和数据,或者取决于应用程序的结构。

For the second topic, here is a good SO question/answer which explains it pretty well. 对于第二个主题, 这是一个很好的SO问题/答案 ,很好地解释了它。

In angular UI there's a component (Angular UI) that checks whether the route is currently active or not: Here's an example from its documentation: Angular-UI Utils 在angular UI中,有一个组件(Angular UI)用于检查路由当前是否处于活动状态:这是其文档中的示例: Angular-UI Utils

<a ui-route="/page" ng-class="{active: $uiRoute}">link</a>

What it simply means is, when the route in the ui-route is currently accessed, the class active should be enabled. 它的简单含义是,当当前访问ui-route中的路由时 ,应该启用活动类。

If you wanna do it using angular only, you can do it like this. 如果您只想使用angular进行操作,则可以这样做。

<li><a ng-href="/home" ng-class="{'active: $location.path() ==='/home'}">Home</a></li>

This is pretty easy with Angular. 使用Angular相当简单。

You can define a controller for the NavBar as below, 您可以如下所示为NavBar定义一个控制器,

<ul nav-menu ng-controller="NavbarCtrl">
    <li ng-class={active : isRouteActive("/home")}><a href="/home">Index</a></li>        
</ul>

Then your controller should defined as below, 然后,您的控制器应定义如下,

app.module("myApp",["ngRoute"])
  .controller("NavbarCtrl", function($scope, $location) {

   $scope.isRouteActive = function(route) { 
        var curRoute = $location.path();
        return curRoute.match(route);
    }

});

If you are using UI Router it's just a matter of adding an attribute like below, 如果您使用的是UI Router,则只需添加以下属性,

<ul class="nav navbar-nav">
            <li ui-sref-active="active"><a ui-sref="home">Home</a></li>
            <li ui-sref-active="active"><a ui-sref="about">About</a></li>
        </ul>

This will make the item highlighted automatically when the current state matches. 当前状态匹配时,这将使该项目自动突出显示。

Hope this helps 希望这可以帮助

~Ragesh 〜拉吉什

In controller that is attach to menu, inject $state . 在附加到菜单的控制器中,注入$state This will give information about state You are in right now. 这将提供有关您当前所处状态的信息。 ng-class="active: $state === actual_state_name" will set active class . ng-class="active: $state === actual_state_name"将设置活动类。 I guess after inject You can use $state in view, but You need to check it by Yourself. 我想在注入之后您可以在视图中使用$ state,但是您需要自己检查它。

Another solution, not in angular way is getting javascript window object. 另一个解决方案,而不是角度的方式是获取javascript window对象。 Similar is $location, whitch provide info about actual link and other cool features 类似的是$ location,whitch提供有关实际链接和其他出色功能的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM