Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ng显示和本地存储行为异常_Javascript_Angularjs_Html_Local Storage - Fatal编程技术网

Javascript ng显示和本地存储行为异常

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

我只是试图在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 </li>

这不起作用有什么特别的原因吗?哪里出错了?

localStorage
只能存储字符串值

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

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

太多了!我完全没有意识到这一点。甚至检查了dev工具中的结构,并将其与存储字符串的其他键进行了比较,这些字符串确实有“”包装。哦,好吧。Thanx Again将在10分钟内标记为正确答案,因为它允许我您不能使用
!=空
<代码>'null'==null为
false
return localStorage.getItem('articles') !== 'null';