Damian Hetman Damian Hetman - 20 days ago 8
AngularJS Question

Ionic Toggle and Angular with localStorage

I have problem with save values to localStorage with ionic toggle. I tried many thing. One of the solution works on web browser but not on device.

Here is my code:

<ion-toggle ng-model="enable" ng-change="toggle()" toggle-class="toggle-balanced" value="{{enable\}}">I want notification</ion-toggle>


Here is my angular:

var enable = window.localStorage.getItem('enable');

if (enable=='true') { enable=true; } else if (enable=='false') { enable=false; }

$scope.enable = enable;

$scope.toggle = function() {
if ($scope.enable == false) {
window.localStorage.setItem('enable', true);
} else {
window.localStorage.setItem('enable', false);
}
};

Answer

Did you try to use?

var item = JSON.parse(localStorage.getItem("item"));
localStorage.setItem("item", JSON.stringify(item));