显示基于$ localStorage的字段

时间:2017-03-21 19:37:41

标签: javascript angularjs ng-storage

我想根据$ localStorage值显示页面中的字段。

如果我有以下内容:

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

只有当$ localStorage的值不是List 3时,我才会显示List 40

我该怎么做?

2 个答案:

答案 0 :(得分:1)

首先,您需要在控制器中注入$ window以使用localStorage

//here you need to set the varialble which is ListisnotZero  
  $window.localStorage.setItem("ListisnotZero", ListisnotZero);
//then get it from the localstorage
    $scope.ListisnotZero= $window.localStorage.getItem("ListisnotZero");


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

答案 1 :(得分:1)

您可以从$ localStorage获取值并设置为范围变量并使用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>