登录后AngularJS ui-router重定向

Val*_*ter 3 angularjs angular-ui-router rootscope

我有一个使用AngularJS和ui-router的项目.除了从登录屏幕重定向到第一次加载时的预览状态,一切正常.

默认状态为home - http:// test /#/

例如,如果用户未登录 - http:// test /#/ test/1000/details将转到登录页面(它应该执行此操作)

然后在用户登录后,系统进入默认状态"home - http:// test /#/ ",但我想转到http:// test /#/ requests/1000/details

登录后如何保存" http:// test /#/ requests/1000/details或stateName"以重定向?

我尝试使用$stateChangeSuccess保存状态日志,$rootscope但第一个(http:// test /#/ requests/1000/details)永远不会保存.

任何想法如何处理这个?

谢谢.

Joe*_*nek 7

您可以在main-app的run方法中为$ state添加'previous'属性.

这就是我解决问题的方法:

// add ui-router variables to $rootScope. Comes handy in many cases, for example setting page title
angular.module('app').run(['$rootScope', '$state', '$stateParams', addUIRouterVars]);

function addUIRouterVars($rootScope, $state, $stateParams) {
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;

    // add previous state property
    $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState) {
        $state.previous = fromState;
    });
}
Run Code Online (Sandbox Code Playgroud)