Hi guys am a beginner in mean stack development I have tried to refresh the page after logout.I have tried location.reload()
; but it doesn't work tell me the possible code for page reload in this scenario
嗨伙计们是平均堆栈开发的初学者我试图在logout后刷新页面。我尝试过location.reload();但它不起作用告诉我在这种情况下页面重新加载的可能代码
$rootScope.$on('$routeChangeStart', function (event) {
var storage = userService.isLoggedIn();
console.log(storage);
if (!storage) {
console.log('DENY');
$rootScope.adminlogin = "adminlogin";
console.log($rootScope.adminlogin);
$location.path('/login');
$route.reload();
// $state.go('/login', null, {reload: true});
}
else {
console.log('ALLOW');
$rootScope.admindashboard = "admindashboard";
var path = $location.path();
console.log(path);
console.log(storage);
if(path == '/login'){
$location.path('/');
}
}
});
3 个解决方案
#1
0
All you need to do is this little line of Vanilia JS: document.location.href=document.location.href
您需要做的就是Vanilia JS的这一小部分:document.location.href = document.location.href
EDIT: why is this getting downvoted?
编辑:为什么这会被投票?
#2
4
You should use $window.location.reload()
from the the $window service if you want to refresh the page. It does the same thing as the reload button in your browser.
如果要刷新页面,则应使用$ window服务中的$ window.location.reload()。它与浏览器中的重新加载按钮完全相同。
The reason you should use the $window service instead of directly accessing the native window
object as per the AngularJS documentation:
您应该使用$ window服务而不是根据AngularJS文档直接访问本机窗口对象的原因:
While window is globally available in JavaScript, it causes testability problems, because it is a global variable. In AngularJS we always refer to it through the $window service, so it may be overridden, removed or mocked for testing.
虽然窗口在JavaScript中是全局可用的,但它会导致可测试性问题,因为它是一个全局变量。在AngularJS中,我们总是通过$ window服务引用它,因此可以覆盖,删除或模拟测试。
On a side note as you stated you are using the $route
service, just calling $route.reload()
will only reload your controllers and not your entire application.
如你所说,你正在使用$ route服务,只需调用$ route.reload()只会重新加载你的控制器,而不是你的整个应用程序。
#3
0
if you are using routes, then on click of Logout just route it to your login page.
如果您正在使用路线,则单击Logout将其路由到您的登录页面。
Snap shot from demo:
从演示快照:
these are my routes:
这些是我的路线:
$routeProvider
.when('/login', {
controller: 'LoginController',
templateUrl: 'modules/authentication/views/login.html',
hideMenus: true
})
.when('/', {
controller: 'HomeController',
templateUrl: 'modules/home/views/home.html'
})
.otherwise({ redirectTo: '/login' });
and when i click on 'Logout' on my page it should do somehting like:
当我点击我的页面上的“退出”时,它应该做的事情如下:
<p><a href="#/login">Logout</a></a></p>
it should redirect to login page as per routes.
它应该根据路由重定向到登录页面。
#1
0
All you need to do is this little line of Vanilia JS: document.location.href=document.location.href
您需要做的就是Vanilia JS的这一小部分:document.location.href = document.location.href
EDIT: why is this getting downvoted?
编辑:为什么这会被投票?
#2
4
You should use $window.location.reload()
from the the $window service if you want to refresh the page. It does the same thing as the reload button in your browser.
如果要刷新页面,则应使用$ window服务中的$ window.location.reload()。它与浏览器中的重新加载按钮完全相同。
The reason you should use the $window service instead of directly accessing the native window
object as per the AngularJS documentation:
您应该使用$ window服务而不是根据AngularJS文档直接访问本机窗口对象的原因:
While window is globally available in JavaScript, it causes testability problems, because it is a global variable. In AngularJS we always refer to it through the $window service, so it may be overridden, removed or mocked for testing.
虽然窗口在JavaScript中是全局可用的,但它会导致可测试性问题,因为它是一个全局变量。在AngularJS中,我们总是通过$ window服务引用它,因此可以覆盖,删除或模拟测试。
On a side note as you stated you are using the $route
service, just calling $route.reload()
will only reload your controllers and not your entire application.
如你所说,你正在使用$ route服务,只需调用$ route.reload()只会重新加载你的控制器,而不是你的整个应用程序。
#3
0
if you are using routes, then on click of Logout just route it to your login page.
如果您正在使用路线,则单击Logout将其路由到您的登录页面。
Snap shot from demo:
从演示快照:
these are my routes:
这些是我的路线:
$routeProvider
.when('/login', {
controller: 'LoginController',
templateUrl: 'modules/authentication/views/login.html',
hideMenus: true
})
.when('/', {
controller: 'HomeController',
templateUrl: 'modules/home/views/home.html'
})
.otherwise({ redirectTo: '/login' });
and when i click on 'Logout' on my page it should do somehting like:
当我点击我的页面上的“退出”时,它应该做的事情如下:
<p><a href="#/login">Logout</a></a></p>
it should redirect to login page as per routes.
它应该根据路由重定向到登录页面。