【问题标题】:ionic local storage and checkbox离子本地存储和复选框
【发布时间】:2016-03-05 00:58:10
【问题描述】:

我在模式中创建了一个复选框: 但是当我关闭并重新打开模式时,已检查的项目不会显示为已检查。这是我的代码

<div class="list">
 <ion-checkbox ng-repeat="thema in themen track by $index"
   type="checkbox"
   id="{{thema.id}}"
   ng-model="thema.checked"
   ng-checked="{{getCheck($index)}}">
   {{ thema.name }}
 </ion-checkbox>
</div>

在我添加的js文件中

$scope.openModal = function() {
 $scope.modal.show();
  $scope.themen = [
   { name: 'something', id: 1 },
   //some other objects
   ];
};
  $scope.closeModal = function() {
    $scope.updateThemaLocalStorage = function ($index) {
       $window.localStorage.setItem( $index, $scope.themen[$index].checked );
   };
    $scope.modal.hide();
  };

也在gulp watch 我收到error "$index" is defined but never used no-unused-vars

【问题讨论】:

    标签: angularjs checkbox ionic local-storage


    【解决方案1】:

    缺少从LocalStorage检索数据的代码,所以假设类似于以下几行:

    for (var i = 0; i < $scope.themen.length; i++) {
        $scope.themen[i].checked = $window.localStorage.getItem(i);
    }
    

    此代码不起作用,因为 checked 属性存储为字符串,因此您需要转换为布尔值:

    for (var i = 0; i < $scope.themen.length; i++) {
        $scope.themen[i].checked = toBool($window.localStorage.getItem(i));
    }
    

    ...

    function toBool(a) {
        if (a=="true") return true; else return false;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 2014-03-31
      • 2017-04-19
      • 2023-03-05
      相关资源
      最近更新 更多