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

标签 javascript angularjs html ionic-framework

这是我的代码,我正在检查 null 或未定义的值或“”,如下所示

我想检查 ionic v1 应用程序是否已登录。

$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();

这里的问题是每次应用程序都被重定向到登录页面,即使项目用户名已设置或用户已登录。解决方案是什么,请帮助我,我是这个 ionic 和 angularjs 的新手。提前致谢。

最佳答案

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

window.localStorage.setItem("用户名", $scope.userinfo.username);

此外,请检查 $scope.userinfo.username 在保存时是否具有正确的值。

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

关于javascript - 如何检查 ionic 本地存储中的 null 或未定义值并重定向到登录页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46282195/

相关文章:

javascript - jQuery 选择器由 a、class、href 组成

JavaScript 函数数据处理

java - 如何使用 HTML 和 CSS 作为 Java 应用程序 GUI?

javascript - 无法从 Controller 中的函数获取数据以进行 ng-repeat

javascript - 具有多个 Controller 的 Angular 共享模型

html - 如何加载自定义字体?

javascript - 我需要一个函数,它为属于某个范围的给定 float 输出十六进制值

angularjs - Youtube iframe Api 和 Angularjs 路线

javascript - Bootstrap 下拉菜单表现得很奇怪

html - 图像不粘在页面底部