如果LocalStorage值为null,则更改位置

时间:2015-05-29 08:08:02

标签: javascript jquery html angularjs html5

我有一个登录页面。登录后,我将用户值存储到会话存储中,如

localStorage.setItem("user", something);

成功登录后,我更改了$location.path('/something')。在此页面中,我指定了$scope.user=localStorage.getItem("user")。如果假设用户删除历史记录并重新加载成功的登录页面(/something),我收到错误。

我试过了:

if (!localStorage.getItem("user")) {
    $scope.$apply(function(){
        $location.path('/');
    })
}

但它不起作用。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

if(typeof localStorage.getItem("user") === 'undefined') {
  //do redirect
}
相关问题