我想根据$ 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 4
和0
。
我该怎么做?
答案 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>