ng-show和localStorage行为奇怪

ng-show and localStorage behaving odd

本文关键字:localStorage ng-show      更新时间:2023-09-26

我只是想隐藏的东西,当一个localStorage键被设置为null,并显示它时,关键不为空。键被称为articles,我试图检查的方式是:

$scope.isEmpty = function isEmpty() {
  return localStorage.getItem('articles') !== null;
};

then in my html:

<li ng-show="isEmpty()"> There is actually something to see here </li>

本地存储键(从chrome开发工具)看起来像这样:

**key**         **Value**
articles          null

有什么特别的原因导致这不起作用吗?我哪里做错了?

localStorage只能存储字符串值。

localStoragenull存储为字符串。当你使用严格比较运算符时,它将不起作用。您可以使用严格比较的字符串'null'

return localStorage.getItem('articles') !== 'null';