简体   繁体   中英

AngularJS $location.path(url) not Redirecting

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.com

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM