javascriptangularjsng-storage

Show fields based on $localStorage


I want to show fields inn the page based on $localStorage value.

If I have the following :

<div ng-controller="myCtrl">
    <ul>
        <li> List 1 </li>
        <li> List 2 </li>
        <li> List 3 </li>
        <li> List 4 </li>
    </ul>
</div>

And I want to show the List 3 and List 4 only if the value of $localStorage is not 0.

How do i do that ?


Solution

  • You can get the value from the $localStorage and set to scope variable and check for the variable using ng-if

    $scope.condvalue = localStorage.getItem("yourItem");
    

    HTML:

    <div ng-controller="myCtrl">
        <ul>
            <li> List 1 </li>
            <li> List 2 </li>
            <li ng-if="condvalue!=0"> List 3 </li>
            <li ng-if="condvalue!=0">  List 4 </li>
        </ul>
    </div>