Vote count:
0
So I fully accept I might be doing something really, really dumb here, but here is goes.
I'm storing my users gender in localStorage using a $localstorage api that has set() and get() methods. I'm using an angular range slider in the UI, just because it looks nice.
This is the code in the controller that is supposed to set the gender when you start the program, and the function that is supposed to store the gender.
From the ionic formulas page. I don't get this bit:
get: function(key, defaultValue) {
return $window.localStorage[key] || defaultValue;
},
I know it creates a get() method on $localstorage but I'm having some trouble getting it working and it's because I don't understand it.
If I use this
$localstorage.get('gender');
what happens if there is nothing stored? It looks like the method get()s the value from localStorage OR gets the defaultValue.
So if I do this:
get: function(key, defaultValue) {
return $window.localStorage[key] || 0;
},
I should get 0 as my default when nothing is stored, yes? Or do I have to do something else?
I'm trying to use this to store the user gender. I initialise it like this:
$scope.usergender = $localstorage.get('storeGender');
console.log("Usergender on initialise is", $scope.usergender);
and then store it like this:
changeGender = function(){$localstorage.set('storeGender',$scope.usergender)
console.log("Usergender is now", $scope.usergender);
}
My html looks like this:
<div class="item range range-assertive">
<i class="icon ion-male"></i> Male
<input type="range" name="gender" min="0" max="1" ng-model="usergender" onChange="changeGender()">
Female <i class="icon ion-female"></i>
</div>
At the moment all I get in the console is:
Usergender on initialise is undefined.
and this remains this way even after I use the range slider to try and change things. I've tried using ng-change and get the same thing.
Help?
Angular model not updating into localStorage via onChange or ng-change
Aucun commentaire:
Enregistrer un commentaire