注销后在 angular js 中重新加载页面

reload page in angular js after logout

大家好,我是平均堆栈开发的初学者 我在 logout.I 尝试 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('/');
         }


    }
});

如果您想刷新页面,您应该使用 $window 服务中的 $window.location.reload()。它的作用与浏览器中的重新加载按钮相同。

您应该使用 $window 服务而不是根据 [=28 直接访问本机 window 对象的原因=] 文档:

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.

旁注,如您所述,您正在使用 $route service, just calling $route.reload() 只会重新加载您的控制器,而不是整个应用程序。

您需要做的就是这一行 Vanilla JS:document.location.href=document.location.href

编辑:为什么这会被否决?

如果您使用的是路由,则单击“注销”即可将其路由到您的登录页面。

演示快照:

这些是我的路线:

$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' });

当我点击我页面上的 'Logout' 时,它应该会做一些类似的事情:

<p><a href="#/login">Logout</a></a></p>

它应该根据路由重定向到登录页面。