Store "scope value" in localstorage?


#1

Hi , I’m having a trouble in storing the scope value in the localstorage , please help me thanks :smile:

$scope.value = 0;

//save scope value
localStorage.setItem($scope.value);

//load scope value
localStorage.getItem($scope.value);

#2

you need to set a key where the value gets stored:

localStorage.setItem('test', $scope.value);
$scope.value = localStorage.getItem('test');

#3

oh , I get it the i will store the $scope.value to the test and store it to localstorage Thanks @bengtler :smile:


#4

I have one more question , when I close my app , and open it again all the data I save in my localstorage are all reset , how can I all retrieve it and set it permanently in my app ? Please help me. Thanks

  // outside my controller
        if ($scope.currentScore > 0 ) {
           $scope.currentScore = window.localStorage.getItem('bestscore');
        }else{
            $scope.currentScore = 0 ;
            
        }

 $scope.getQuestion = function() {
             

                var q = quizFactory.getQuestion($scope.id);
                
            
          
                if(q) {

                    $scope.question = q.question;
                    $scope.options = q.options;
                    $scope.answer = q.answer;
                    $scope.answerMode = true;

                     if($scope.life == 0) {
                   $scope.quizOver = true;
            }
                } else {
                    $scope.currentScore = $scope.score;
                    $scope.highscore;

                    if($scope.score >= 2) {

                       $scope.quizSuccess = true;
                        if ($scope.currentScore > $scope.highscore){
                        window.localStorage.setItem('bestscore', $scope.currentScore);
                        
                    }
                    }else{
                        $scope.quizOver = true;
                        if ($scope.currentScore > $scope.highscore){
                          window.localStorage.setItem('bestscore', $scope.currentScore);
                         
                    }
                    }
                  
                }

            };

#5

if you do not clear your appdata or cache … localstorage data should be there… also after a restart of the app Oo


#6

Is there any solution In disabling the clearing cache in my app ?


#7

ehm you can not active/deactivate that… by default all data will be stored until you explicit delete them.


#8

but I’m having a problem , all the data i stored in my localstorage are all reset when I close my app and Open it again. Please help me , Is there any problem in my code ?


#9

i am using:

for handling localstorage stuff maybe they handled it a litte bit different.

But i see nothing that could cause that in your code :confounded:


#10

Okay I’ll just try , by the way thanks for your help :smile:


#11

I also recommend angular localForage is based in a popular library that interacts with all kind of browser storage and have a deep integration with angular and also allows auto bind of variables to localforage.

example from doc

$localForage.bind($scope, {
key: ‘myStorageKey’, // required
defaultValue: {test: ‘my test’}, // a default value
scopeKey: ‘myObj.myVar’, // the name of the scope key (if you want it to be different from key)
name: ‘myApp’ // instance name
});


#12

Do note that while local storage is generally more permanent than other things, it’s still volatile. For example on iOS, if the user doesn’t have enough space on their phone or gets close to running out it will automatically clear all local storage in any web browsers including hybrid apps. A user can clear their local storage also.

There are some local database alternatives if you need something that has to be persistent.


#13

Is it okay if I just use SQLITE in saving Data ? not localstorage anymore?


#14

Yes, you can use SQLite to store it instead of using localStorage.

I have a post on my blog that should help you with this:


#15

You must understand both localStorage and sessionStorage when working with ionic

// store data to sessionStorage
localStorage.setItem(‘key’, ‘value’);

// Get data from sessionStorage
var data = localStorage.getItem(‘key’);

Use $Scope.Variable in value

You can learn more about localStorage and SessionStorage here - http://goo.gl/9XJcgn


#16

Best solution us to save your questions in the databases using $cordovaSQLite .


#17

Exactly, that should be a lot more consistent and it’s not much different to use.


#18

Wow, I’m so thankful to all of you guys , Thanks for helping me :smile:


#19

Or, if you would love to work with something nosql, you can try lokijs it stores using several methods available. And it’s fast. Since its an in memory db.


#20

Thank @kiongkiong I’ll Try this :smile: