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

2 Answers 2

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);
    }
  };
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.