Search code examples
angularjslocal-storageng-hide

Angular - ng-hide with localStorage doesn't work


I have in my HTML :

<div ng-hide="token">
  Test
</div>

And in my controller :

$scope.token = localStorage.key;

But it always return false (?) - it doesn't work.

And if I try to do it like this :

<div ng-hide="token()">
  Test
</div>

And in Controller :

$scope.token = function(){
    return localStorage.key;
}

it;s also doesn't work.

why ?

And how do i do it work ?

thanks


Solution

  • You're not using localStorage right. The right method is:

    $scope.token = localStorage.getItem(key);
    

    Similarly, you would setItem to put something in, and removeItem to delete it later.

    Complete documentation on localStorage: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage