我现在正在我的 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
标记以使样式保持不变。
关于javascript - 登录成功后如何重新加载/重定向页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25717534/