3

我有一个使用 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)永远不会被保存。

任何想法如何处理这个?

谢谢。

4

1 回答 1

7

您可以在主应用程序的 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;
    });
}
于 2014-12-10T16:03:27.047 回答