显示基于 $localStorage 的字段

Show fields based on $localStorage

我想根据 $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 的值不是 0.

时显示 List 3List 4

我该怎么做?

首先你需要在你的控制器中注入 $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>

您可以从 $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>