Javascript ng显示和本地存储行为异常
我只是试图在localStorage键设置为null时隐藏一些内容,并在该键不为null时显示它。该键被称为Javascript ng显示和本地存储行为异常,javascript,angularjs,html,local-storage,Javascript,Angularjs,Html,Local Storage,我只是试图在localStorage键设置为null时隐藏一些内容,并在该键不为null时显示它。该键被称为articles,我试图检查的方式是: $scope.isEmpty = function isEmpty() { return localStorage.getItem('articles') !== null; }; 然后在我的html中: <li ng-show="isEmpty()"> There is actually something to see here
articles
,我试图检查的方式是:
$scope.isEmpty = function isEmpty() {
return localStorage.getItem('articles') !== null;
};
然后在我的html中:
<li ng-show="isEmpty()"> There is actually something to see here </li>
这不起作用有什么特别的原因吗?哪里出错了?
localStorage
只能存储字符串值
localStorage
将null
存储为字符串。当您使用严格比较运算符时,它将不起作用。您可以使用字符串'null'
进行严格比较
return localStorage.getItem('articles') !== 'null';
太多了!我完全没有意识到这一点。甚至检查了dev工具中的结构,并将其与存储字符串的其他键进行了比较,这些字符串确实有“”包装。哦,好吧。Thanx Again将在10分钟内标记为正确答案,因为它允许我您不能使用
!=空
<代码>'null'==null为false
。
return localStorage.getItem('articles') !== 'null';