Javascript 如何检查ionic本地存储中的空值或未定义值并重定向到登录页面?

Javascript 如何检查ionic本地存储中的空值或未定义值并重定向到登录页面?,javascript,angularjs,html,ionic-framework,Javascript,Angularjs,Html,Ionic Framework,这是我的代码,我正在检查空值或未定义的值,或者类似的 我想在ionic v1应用程序中检查loggedin是否存在 $urlRouterProvider.otherwise(function () { var items = window.localStorage.getItem('username'); if (items === undefined || items === null || items.length === 0) { return '/ap

这是我的代码,我正在检查空值或未定义的值,或者类似的

我想在ionic v1应用程序中检查loggedin是否存在

$urlRouterProvider.otherwise(function () {
    var items = window.localStorage.getItem('username');
    if (items === undefined || items === null || items.length === 0) {


        return '/app/login';


    }
    else {


        return '/app/search';


    }


});
也试过这个

 if (localStorage.getItem("username") === null) {

      console.log($localStorage.username);
      $scope.username = $localStorage.username;
  }
但是没有运气 我在这里设置登录后的值

$localStorage.username = $scope.userinfo.username;
像这样清除本地存储项

  localStorage.removeItem("username");
  $window.localStorage.clear();
  $ionicHistory.clearCache();
  $ionicHistory.clearHistory();
这里的问题是每次应用程序被重定向到登录页面时,即使设置了项目用户名或用户已登录。有什么解决办法请帮助我,我是新来的爱奥尼亚和安格拉斯。提前感谢。

您的问题在这里$localStorage.username=$scope.userinfo.username;。localStorage.username似乎设置不正确。您可以尝试:

window.localStorage.setItemusername,$scope.userinfo.username

另外,检查保存时$scope.userinfo.username的值是否正确


编辑:顺便说一句,我希望您仅在用户注销时清除localstorage.username,对吗?

localstorage.getItemusername===null应该可以工作…如果不是,则可能有一个空字符串在其中。使用console.log检查您在LocalStorages上的值非常感谢您,它起了作用。我已经抓狂了2天了。顺便说一句,我正在使用这个localStorage.removietemusername$window.localStorage.clear$ionicHistory.clearCache$ionicHistory.clearHistory;在注销时这是正确的方法。如果只是在注销时就可以了。我问你,因为你的问题可能是因为你可能在其他时候也打过电话。但是如果修好了,那就好了。非常感谢