离子本地存储和复选框

时间:2015-12-01 07:19:22

标签: angularjs checkbox ionic local-storage

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

<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

1 个答案:

答案 0 :(得分:1)

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

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

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

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;
}