0

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

如果我有以下情况:

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

而且我想显示List 3andList 4仅当 $localStorage 的值不是时0

我怎么做 ?

4

2 回答 2

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>
于 2017-03-21T19:42:48.647 回答
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>
于 2017-03-21T19:43:01.373 回答