UI路由器$ state.go不重定向?

Mik*_*ner 4 javascript angularjs angular-ui-router

这是一个常见的问题,我看到它很多但似乎没什么用.这就是我正在做的事情.我希望我的状态有一些动态动画,所以基本上登录会做一些很酷的动画并进入实际的界面.现在,我开始嵌套这样的视图:

    $stateProvider
        .state('login', {
            url: '/login',
            title: "Login",
            views: {
                "master": {
                    controller: "LoginController",
                    templateUrl: "/components/login/login.html",
                    authentication: false
                }
            }
        })
        .state("logout", {
            url: "/logout",
            title: "Logout",
            authentication: false
        })
        .state('foo', {
            url: '/',
            controller: "HomeController",
            views: {
                "master": {
                    templateUrl: '/components/ui-views/masterView.html'
                },
                "sidebar@foo": {
                    templateUrl: '/components/sidebar/_sidebar.html'
                },
                "header@foo": {
                    templateUrl: '/components/header/_header.html'
                }
            }
        })
        .state('foo.inventory', {
            url: '/inventory',
            title: "Inventory",
            views: {
                "content@foo": {
                    controller: "InventoryController",
                    templateUrl: "/components/inventory/inventory.html"
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)

因此,在运行此操作时,我需要重定向到注销,但它会卡住.它根本不会从此注销状态移动.以下是我处理的方法:

function run($http, $rootScope, $cookieStore, $state, $templateCache, $timeout, AuthService, modalService, const) {
        var timeout;

        FastClick.attach(document.body);
        $rootScope.globals = $cookieStore.get('globals') || {};

if ($state.current.name !== 'login' && !$rootScope.globals.guid) {
            $state.go('login');
        } else if ($state.current.name == 'login' && $rootScope.globals.guid) {
            $state.go('foo');
        }

        $rootScope.$on('$stateChangeStart', function (event, next, current, fromState, fromParams) {
            var authorizedRoles = next.level != undefined ? next.level : 1,
                needAuth = next.authentication != undefined ? next.authentication : true;

            if (needAuth) {
                if (!AuthService.isAuthorized(authorizedRoles, true)) {
                    event.preventDefault();
                    if (AuthService.isAuthenticated()) {
                        $rootScope.$broadcast(const.auth.notAuthorized);
                        $state.go('foo', {});
                    } else {
                        $rootScope.$broadcast(const.auth.notAuthenticated);
                    }
                }
            }

            if (next.name == 'logout') AuthService.logout($rootScope.globals);
        });

}
Run Code Online (Sandbox Code Playgroud)

那为什么这不起作用?看起来这个工作很好.但是$state.go('login')回报价值不好.

如果有人能指导我朝正确的方向发展,或者告诉我究竟是什么问题.

Mik*_*ner 7

问题是$ state.go正在运行,似乎没有关于这个主题的很多文档.$ state.go尚未初始化,不会运行.


Asa*_*kar 6

我有同样的问题.在调试属性如何ui-sref为超链接工作之后,我发现$state.go核心angular-ui库中的$ timeout被包围了.

var transition = $timeout(function() {
                debugger;
                $state.go("app.authentication");
            });
Run Code Online (Sandbox Code Playgroud)

也许你可以尝试相同的.(虽然核心库中的ui-sref事件处理程序清楚地提到这是一个黑客.)