【问题标题】:Show fields based on $localStorage显示基于 $localStorage 的字段
【发布时间】:2017-03-21 19:37:40
【问题描述】:

我想根据 $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

我该怎么做?

【问题讨论】:

  • 请向我们展示您的控制器

标签: javascript angularjs ng-storage


【解决方案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>

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我认为他们在谈论ngStorage(顺便说一句,我没有投反对票)
    • @AlonEitan LOL 感谢您让我知道 idk 现在在堆栈中,即使共享知识也变得危险
    • 别担心 - 我已经解决了:)
    • @AlonEitan thnk u bud :P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多