UI 路由器 $state.go 未重定向

UI Router $state.go is not redirecting?

本文关键字:重定向 go state 路由器 UI      更新时间:2023-09-26
这是一个

常见问题,我看到了很多,但似乎没有任何效果。这就是我正在做的事情。我想在我的状态中有一些动态动画,所以基本上登录会做一些很酷的动画并进入实际界面。现在,我开始像这样嵌套视图:

    $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"
                }
            }
        });

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

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);
        });
}

那么为什么这行不通呢?看起来这工作正常。但$state.go('login')返回一个错误的值。

如果有人能引导我朝着正确的方向前进,或者告诉我到底出了什么问题。

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

我遇到了同样的问题。在调试了属性ui-sref如何用于超链接后,我发现$state.go在核心 angular-ui 库中环绕着$timeout。

var transition = $timeout(function() {
                debugger;
                $state.go("app.authentication");
            });

也许你可以尝试同样的方法。(尽管核心库中的 ui-sref 事件处理程序明确提到这是一个黑客。

只需要升级ui-router版本就可以了,见连接地址:https://github.com/mattlewis92/angular-bluebird-promises/issues/11

如果你升级到ui-router 0.3.2,它实际上已经在那里修复了:angular-ui/ui-router@66ab048

我在应用程序中遇到了这个问题,您正在尝试在与状态无关的内部ui-view上调用$state.go,这就是您面临的问题,因此请尝试以下操作:

app.run(['$state', '$rootScope', '$timeout', 
function ($state, $rootScope, $timeout) {
 $timeout(function() { 
      $state.go('login');
    });   
}]);