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
Add a ment  | 

3 Answers 3

Reset to default 7

Try $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