成功登录后如何重新加载/重定向页面?

时间:2014-09-08 04:37:01

标签: javascript angularjs

我目前正在我的控制器中使用此功能:(刚开始没有打败我)

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();
            });
        }
    }
}]);

哪个工作正常,当我重新加载页面时,导航栏登录/创建链接消失并被注销替换,什么不是,但我的问题是导航栏没有重新加载,除非我手动点击CTRL+R < / p>

我已尝试使用$location.path('/');$route.reload();但我的导航栏仍然无法更改,除非我手动重新加载页面。

1 个答案:

答案 0 :(得分:1)

这可以解决这个问题吗?这应该完全将页面重新加载到新地址。

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();
            });
        }
    }
}]);

您还可以尝试隐藏整个文档,重新下载文档内容,并替换当前页面的内容。

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

替换BODY标记以使样式保持不变。

Replace entire HTML document in-place