相关疑难解决方法(0)

使用$ window或$ location在AngularJS中重定向

我正在处理的应用程序包含各种状态(使用ui-router),其中某些州要求您登录,其他州是公开可用的.

我创建了一个方法来有效地检查用户是否已登录,我当前遇到的问题实际上是在必要时重定向到我们的登录页面.应该注意的是,登录页面当前未放在AngularJS应用程序中.

app.run(function ($rootScope, $location, $window) {


    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {

        if (toState.data.loginReq && !$rootScope.me.loggedIn) {
            var landingUrl = $window.location.host + "/login";
            console.log(landingUrl);
            $window.open(landingUrl, "_self");
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

console.log正确显示了预期的URL.在那之后,我几乎尝试了从$ window.open到window.location.href的所有内容,无论我尝试过什么都没有重定向发生.

编辑(已解决):

发现了这个问题.

var landingUrl = $window.location.host + "/login";
$window.open(landingUrl, "_self");
Run Code Online (Sandbox Code Playgroud)

变量landingUrl设置为'domain.com/login',这不适用于$ window.location.href(这是我尝试过的事情之一).但是在将代码更改为之后

var landingUrl = "http://" + $window.location.host + "/login";
$window.location.href = landingUrl;
Run Code Online (Sandbox Code Playgroud)

它现在有效.

javascript redirect angularjs angular-ui-router

88
推荐指数
3
解决办法
21万
查看次数