angularjs中的重定向状态 [英] Redirect state in angularjs

查看:48
本文介绍了angularjs中的重定向状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是状态配置:

angular
    .module('grabhutApp', [...])
    .config(function ($stateProvider, $urlRouterProvider) {

        $stateProvider
            // ACCOUNT
            .state('account', {
                abstract: true,
                url: '/account',
                templateUrl: 'index.html'
            })
            .state('account.main', {
                url: '',
                templateUrl: 'views/account/account.login.html',
                controller: 'AccountController'
            })
            .
            .
            .
            // NO VIEWS
            .state('nv', {
                abstract: true,
                url: '/nv'
            })
            .state('nv.logout', {
                url: '/logout'
            })  
    });

nv 及其子状态将没有物理视图或控制器.
我希望它们充当调用某些功能的链接.

The nv and its sub states will have no physical views or controllers.
I want them to serve as links that calls certain functions.

调用注销方法的服务:

angular.module('grabhutApp')
    .factory('$grabhutAccountService', function ($state, $grabhutDataService) {

        var methods = {     
            .
            .
            logoutUser: function () {
                $grabhutDataService.user.removeSession();
                $state.go('account.main', {}, {location: 'replace'});
            }
            .
            .
        };

        return methods;
    });

然后是一个用于注销的按钮/链接:

<a ui-sref="nv.logout" class="button icon icon ion-log-out button-large" menu-close></a>  

我想要发生的是,当状态 nv.logout 被触发
$grabhutAccountService.logoutUser() 必须被调用并且必须重定向到 'account.main'

What I want to happen is that, when state nv.logout was triggered the
$grabhutAccountService.logoutUser() must be called and must redirect to 'account.main'

这是我到目前为止所做的:
我尝试在 nv.logout 中使用 resolve

Here is what I've done so far:
I tried to use resolve in nv.logout

.state('nv.logout', {
    url: '/logout',
    resolve: {
        logout: function ($grabhutAccountService) {
            $grabhutAccountService.logoutUser();
        }
    }
}) 

服务被调用,但状态没有重定向.所以我尝试了另一种方法.我添加了一个控制器:

The service was called but state did not redirect. So I tried another way. I added a controller:

.state('nv.logout', {
    url: '/logout',
    resolve: {
        logout: function ($grabhutAccountService) {
            $grabhutAccountService.logoutUser();
        }
    },
    controller: function ($scope, $state) {
        $scope.$on('$stateChangeSuccess', function () {
            $state.go('account.main');          
        });
    }   
})  

但是 $stateChangeSuccess 没有被触发.
所以我尝试使用rootScope:

But $stateChangeSuccess is not being fired.
So I tried to use the rootScope:

.run(function(...., $grabhutAccountService){

    .
    .
    .
    $rootScope.logout = function(){
        $grabhutAccountService.logoutUser();
    };
    .
    .
    .

})  

然后像这样使用它:

<a ng-click="$root.logout()" class="button icon icon ion-log-out button-large" menu-close></a>

这很好用.但我很担心,因为(AFAIK)rootScope 会加载更多数据,这可能会导致操作速度变慢.
此外,每当我需要像上面这样的某种功能时,我必须再次在 rootScope 中附加功能.
而且我认为这不是一个好方法.顺便说一句,我正在 phonegap 中构建它,这就是内存使用如此重要的原因.

This works fine. But I'm worrying since (AFAIK) rootScope loads more data which could cause slower operation.
Besides, whenever I need some kind of function like above, I would have to attach function in rootScope again.
And I think that's not a good approach. BTW, I'm building this in phonegap that's why memory usage is so important.

推荐答案

哦哦,你好近啊.我重新排列了您的一些代码并得出了这个结论:

Ooooh you're so close. I rearranged some of your code and arrived at this:

app.run(function($rootScope, $grabhutAccountService) {
   $rootScope.$on('$stateChangeSuccess', function (evt, toState) {
     if (toState.name === 'nv.logout') {
       $grabhutAccountService.logoutUser();
       $state.go('account.main');
     }  
   });
});

UI-Router 的下一个主要版本将有很多改进的钩子来做这类事情.

The next major version of UI-Router will have much improved hooks for doing this sort of thing.

这篇关于angularjs中的重定向状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆