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

标签 javascript angularjs

我现在正在我的 Controller 中使用它:( Angular 新手不要打败我)

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

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

最佳答案

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

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

关于javascript - 登录成功后如何重新加载/重定向页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25717534/

相关文章:

javascript - AngularJS 中获取点击元素的 ID

由于 innerHTML 的使用,JavaScript onclick 函数未绑定(bind)

javascript - 如何从谷歌电子表格中删除行?

javascript - 单击事件监听器中 undefined variable

javascript - Javascript 中的 XMLRPC 客户端

javascript - 在水平列表上用鼠标滚轮水平滚动

javascript - Uncaught Error : [$injector:modulerr] while using external js file

javascript - AngularJs - ngRepeat 删除 View 中的特定行

angularjs - 我可以将 Twitter Flight 与 AngularJS 结合使用吗?

javascript - Controller 结构的差异