1

I have some problem, in my Ionic app use toggle to switch on/off function. By default toggle is true, when toggle change it switch to false.

<ion-toggle ng-model="myToggle.checked" ng-change="myToggleChange()">
  Toggle
</ion-toggle> 

All work fine when use app, but if I close app and switch is false before closing when I load app again toggle has true by default when my controller start. How cache toggle after reload app has position before closing? Controller:

      $scope.myToggleChange = function() {

        if($scope.myToggle.checked == true){
            console.log('Is True', $scope.myToggle.checked);
        }
        if($scope.myToggle.checked == false){
            console.log('Is False', $scope.myToggle.checked);
        }
      };

      $scope.myToggle = { checked: true };
KingStakh
  • 303
  • 3
  • 13

2 Answers2

2

Resolved by 10 hours reading docs and manuals )))

  $scope.myToggleItem = localStorage.getItem('check');

    if($scope.myToggleItem){    
      var item = localStorage.getItem('check');  
      $scope.myToggle = {checked: JSON.parse(item)};

    } else {    
      $scope.myToggle = { checked: true };
    }

  $scope.myToggleChange = function() {

    if($scope.myToggle.checked == true){
        console.log('Item', $scope.myToggle.checked);
        localStorage.setItem('check', $scope.myToggle.checked);
    }
    if($scope.myToggle.checked == false){
        console.log('Item', $scope.myToggle.checked);
        localStorage.setItem('check', $scope.myToggle.checked);
    }
  };
KingStakh
  • 303
  • 3
  • 13
0

You can use the localstorage to store such data. https://github.com/gsklee/ngStorage

in your change handler you can store the value using

 $localStorage.myToogleValue = $scope.myToggle

you controller can check at the beginning

if ($localStorage.myToogleValue)
    $scope.myToggle = { checked: $localStorage.myToogleValue.checked };
else
    $scope.myToggle = { checked: true };
kafbuddy
  • 304
  • 2
  • 5