这是我的代码,我正在检查 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/