【问题标题】:AngularJS $location.path(url) not RedirectingAngularJS $location.path(url) 不重定向
【发布时间】:2014-05-21 00:52:26
【问题描述】:

全部,

我正在开发一个 AngularJS PhoneGap 应用程序。我被困在收到 API 调用响应后立即发生的一个重定向。我的代码位于gist.github.com

我的问题在 controllers.js 的第 300 行,我在 then() 块中调用我的重定向。

UserService.login($scope.user.email, $scope.user.password)
            .then(function(response) {

                globals.session_id = response.data.meta.session_id;
                globals.logged_in_userId = response.data.response.users[0].id;

                if ($scope.user.remember === true) {
                    window.localStorage.setItem("_session_id", response.data.meta.session_id);
                    window.localStorage.setItem("logged_in_userId", response.data.response.users[0].id);
                } else {
                    window.localStorage.removeItem("_session_id");
                    window.localStorage.removeItem("logged_in_userId");
                }
            })
            .then(function() {
                $location.path("/tab/locations");// <=== This line doesn't cause an error but does not result in a redirect
            })
            .
        catch (function(response) {
            alert(JSON.stringify(response));
        });

如果有人可以帮助我,我将不胜感激!

谢谢, 布鲁斯

【问题讨论】:

  • 到目前为止,我已经尝试过使用 rootScope apply、scope apply,并在 promise return 中使用变量以及 scope watch,但重定向始终不起作用。我也尝试过使用 window.navigation.href,但我仍然一无所获。成功登录后必须有一种方法来重定向用户。

标签: javascript angularjs cordova location


【解决方案1】:

试试 $rootScope.$apply();在 $location.path() 之后

或

$scope.$apply(function() {
  $location.path("/tab/locations");
});

你也可以使用 $location.url()

【讨论】:

  • 您好 Abhishek 感谢您的回复。不幸的是,我尝试了这两种方法都没有运气。
  • 为我工作。谢谢!
【解决方案2】:

您的then 处理程序需要返回一些内容,以便承诺将在下一个条件下停止解析。

只需在 then 处理程序的末尾添加 return response。

【讨论】:

  • 您好,Alp,感谢您的回复。我刚刚尝试过,但仍然没有重定向。
  • 如果你使用window.location.href = "/tab/locations",它是否有效?
【解决方案3】:

看来问题实际上出在我的 app.js 运行函数调用中。在位置更改时,它会查看全局变量以查看是否已设置用户名,如果尚未设置,则拦截将客户端发送到登录页面的调用。一旦我将代码移到 OnLocationChange 处理程序中以查看那里的变量,它就会正确重定向。

谢谢, 乙

【讨论】:

    猜你喜欢
    • 2015-06-17
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多