简体   繁体   English

如何使用 angular.js 在锚标记内设置先前的状态

[英]How to set previous state inside anchor tag using angular.js

I need to set the just previous state inside the anchor tag while navigating the menu.我需要在导航菜单时设置锚标记内的前一个状态。 Here is my code:这是我的代码:

<div id="navbar" class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <li ui-sref-active="active"><a ui-sref=".profile">College Profile</a></li>
        <li ui-sref-active="active"><a ui-sref=".stream">College stream</a></li>
        <li ui-sref-active="active"><a ui-sref=".dept">College Department</a></li>
        <li class="dropdown " ui-sref-active="active">
            <a ui-sref="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Resource Management <span class="caret"></span></a>
            <ul class="dropdown-menu">
                <li><a ui-sref="#">Add User Role</a></li>
                <li><a ui-sref="#">Add Course</a></li>
                <li><a ui-sref="#">Add Section</a></li>
                <li><a ui-sref="#">Add Session</a></li>
                <li><a ui-sref="#">Add Semester</a></li>
                <li><a ui-sref="#">Add Unit</a></li>
            </ul>
        </li>
        <li ui-sref-active="active"><a ui-sref=".usermanagement">User Management</a></li>
        <li ui-sref-active="active"><a ui-sref=".role">User Role</a></li>
    </ul>
</div>

In this code I need when user will click on "Resource Management" menu.在此代码中,当用户单击“资源管理”菜单时,我需要。 This menu will active and other menus will not active (here I am using active class for highlight the menu) and will show the just previous state page.此菜单将处于活动状态,而其他菜单将不会处于活动状态(这里我使用活动类来突出显示菜单)并将显示前一个状态页面。

As it has some sub menus these sub menus will display in dropdown list.由于它有一些子菜单,这些子菜单将显示在下拉列表中。 In this case all is coming properly but it's throwing the below error and cursor:pointer property also not coming on this menu.在这种情况下,一切正常,但它会抛出以下错误,并且cursor:pointer属性也没有出现在这个菜单上。

Error:错误:

Error: Could not resolve '#' from state 'dashboard'
    at Object.t.transitionTo (angularuirouter.js:7)
    at Object.t.go (angularuirouter.js:7)
    at angularuirouter.js:7
    at angularjs.js:146
    at e (angularjs.js:43)
    at angularjs.js:45

ui-router doesn't track the previous state once it transitions, but the event $stateChangeSuccess is broadcast on the $rootScope when the state changes. ui-router 一旦转换就不会跟踪先前的状态,但是当状态发生变化时,会在 $rootScope 上广播事件$stateChangeSuccess

You should be able to catch the prior state from that event (" from " is the state you're leaving):您应该能够从该事件中捕获先前的状态(“ from ”是您要离开的状态):

app.run(['$rootScope',function($rootScope)
{  
  $rootScope.previousState;
    $rootScope.currentState;
        $rootScope.$on('$stateChangeSuccess', function(ev, to, toParams, from, fromParams) {
            $rootScope.previousState = from.name;
            $rootScope.currentState = to.name;
            console.log('Previous state:'+$rootScope.previousState)
            console.log('Current state:'+$rootScope.currentState)
        });
}]);

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

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