简体   繁体   中英

How to reload / redirect page after successful login?

I'm currently using this in my controller right now: (new to angular don't beat me)

app.controller('userLogin', ['$scope', '$http', '$location', '$route', function ($scope, $http, $location, $route) {
    activeLink = 'userLogin';
    $scope.submitForm = function(valid) {
        if (valid) {
            $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
            $http.post('/user/login', $('#userLoginForm').serialize())
            .success(function(data) {
                $route.reload();
            });
        }
    }
}]);

Which works fine, when I reload the page the navbar login/create links disappear and is replaced with logout and what not, but my problem is the navbar doesn't reload unless I manually hit CTRL+R

I've tried using $location.path('/'); and $route.reload(); but my navbar doesn't change still unless I manually reload the page.

Would this do the trick? This should completely reload the page to the new address.

window.location.replace("/");

How to redirect to another webpage in JavaScript/jQuery?

app.controller('userLogin', ['$scope', '$http', '$location', '$route', function ($scope, $http, $location, $route) {
    activeLink = 'userLogin';
    $scope.submitForm = function(valid) {
        if (valid) {
            $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
            $http.post('/user/login', $('#userLoginForm').serialize())
            .success(function(data) {
                window.location.replace("/");
                //$route.reload();
            });
        }
    }
}]);

You could also try to hide the entire document, re-download the content of the document, and replace the content of current page.

$.get("/", function(data){
    $('body').html(data.body); // not sure if data.body will work, sort of guessing here
});

jquery append external html file into my page

Replace the BODY tag to make styles stay in place.

Replace entire HTML document in-place

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