admin管理员组文章数量:1306985
All,
I have an AngularJS PhoneGap app I am working on. I am stuck on one redirect that occurs immediately after receiving an API call response. My code is located at gist.github
My issue is on line 300 of controllers.js, where I am calling my redirect within a then() block.
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));
});
If anyone could please help me out, I'd appreciate it!
Thanks, Bruce
All,
I have an AngularJS PhoneGap app I am working on. I am stuck on one redirect that occurs immediately after receiving an API call response. My code is located at gist.github.
My issue is on line 300 of controllers.js, where I am calling my redirect within a then() block.
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));
});
If anyone could please help me out, I'd appreciate it!
Thanks, Bruce
Share Improve this question asked May 21, 2014 at 0:52 jaxmeierjaxmeier 5094 silver badges14 bronze badges 1- So far I've tried using rootScope apply, scope apply, and using a variable inside the promise return along with scope watch, and at no time does the redirect work. I have also tried using window.navigation.href, and I still get nothing. There has to be a way to redirect the user after successfully logging in. – jaxmeier Commented May 23, 2014 at 16:28
3 Answers
Reset to default 7Try $rootScope.$apply(); after $location.path()
OR
$scope.$apply(function() {
$location.path("/tab/locations");
});
You can also use $location.url()
Your then
handler needs to return something so that the promise will be stilled resolve for the next condition.
Just add return response
at the end of your then
handlers.
It appears the issue was actually in my app.js run function call. On location change it looks at a global variable to see if a username has been set yet, and if not, it intercepts the call to send the client to the login page. Once I moved my code inside the OnLocationChange handler to look at the variable there, it redirected properly.
Thanks, B
本文标签: javascriptAngularJS locationpath(url) not RedirectingStack Overflow
版权声明:本文标题:javascript - AngularJS $location.path(url) not Redirecting - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741832384a2400009.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论