1

I am just trying to hide something when a localStorage key is set to null, and show it when the key is not null. The key is called articlesand the way I am trying to check is:

$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>

the local storage key (from chrome dev tools) looks like this:

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

is there any particular reason why this isnt working? Where did I go wrong?

letterman549
  • 311
  • 2
  • 16

1 Answers1

3

localStorage can only store string values.

localStorage stores null as string. When you use strict comparison operator it'll not work. You can use string 'null' with strict comparison.

return localStorage.getItem('articles') !== 'null';
Tushar
  • 85,780
  • 21
  • 159
  • 179