删除浏览器数据后,ngstorage的$ localStorage仍以角度显示

时间:2018-05-04 20:18:07

标签: angularjs angular-ui-router local-storage ng-storage

我正在使用ngStorage将数据存储在本地存储中。我正面临这个奇怪的问题,当我从浏览器中删除本地存储时,我的网站用户没有注册网址更改。

这里是代码,用于检查localstorage是否存在,然后执行相应的路由:

$rootScope.$on('$locationChangeStart', function (event, next, current) {
              //it always by passes this
                if (!$localStorage.currentUser) {

                    $location.path('/login');

                }

            });


只有当我刷新页面时,它才会自动路由到login页面,否则它会继续在网站中的任何路线上进行,而且没有本地存储值。

为什么会发生这种情况?请帮帮我

1 个答案:

答案 0 :(得分:0)

试试这个

//Checks if currentUser property still exists
if (!$localStorage.hasOwnProperty("currentUser")) {
      $location.path('/login');
}