2014-05-21 41 views
2

所有,

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

我的问题是在第300行的controllers.js,我打电话给我的重定向在一个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)); 
     }); 

如果有人可以请帮助我,我会很感激!

感谢, 布鲁斯

+0

使用rootScope到目前为止,我已经试过申请,适用范围,并使用与范围的手表沿承诺回报中的变量,并且在任何时候做重定向工作。我也尝试使用window.navigation.href,我什么都没有。 必须有成功登录后重定向用户的方法。 – jaxmeier

回答

0

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

感谢, 乙

1

then处理程序需要返回的东西,这样的承诺将会停息,决心在未来条件。

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

+0

Hi Alp, 感谢您的回复。我只是试过,但仍然没有重定向。 – jaxmeier

+0

如果使用'window.location.href =“/ tab/locations”',它会起作用吗? – Alp

7

尝试$ rootScope。$ apply(); $ location.path()后

OR

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

您还可以使用$ location.url()

+0

嗨阿布舍克 感谢您的回复。不幸的是,我已经尝试了两种方法,没有运气 – jaxmeier

+1

这对我有效。谢谢! –

+0

为我工作。谢谢! –