Array in session

Friends, I need to keep an arrangement “session” was using window.localStorage but it has not worked well for me, I do the treatment is as follows:

use this service to save and return my array

.service('misCorredoresService',function(){
  var corredores ={};
     return {
               getCorredores:function(){
                    return corredores;
                },
               setCorredores:function(data){
                    corredores = data;
                }
     }
})

After consuming the webservice, I handle the data and assessed whether or not I keep in my arrangement.

$scope.resultados = data.list;
    var agregar = true;
    if($scope.mis_corredores.length <=9){
      if($scope.mis_corredores.length > 0 && $scope.mis_corredores.length <=9){
        for (var i = 0; i <= $scope.mis_corredores.length-1; i++) {
          if($scope.mis_corredores[i].tag == $scope.resultados[0].tag){
            agregar = false;
          }
        };
      }
      if(agregar){
        $scope.mis_corredores.push(data.list[0]);
        misCorredoresService.setCorredores($scope.mis_corredores);
          $ionicPopup.show({
           title: 'Notificación',
           subTitle: '',
           content: 'El corredor se ha agregado a la lista.',
           buttons: [
             {
               text: 'Aceptar',
               type: 'button-positive',
             },
           ]
          })
        }
      }

After that, if the user closed the application I would like to keep the list of users added to the array.

I need help if anyone knows or advise me to do otherwise would greatly appreciate it.

Greetings and thanks for your comments.

up please help me! !!!

Local storage does not support only supports strings, You need to stringify the array before adding it, then parse it when you call it.

getCorredores:function(){
    return JSON.parse(corredores);
},
setCorredores:function(data){
    corredores = JSON.stringify(data);
}

Hope this helps.

Yeah check out this tutorial by the Ionic team for more info http://learn.ionicframework.com/formulas/localstorage/

1 Like

Also try to store it in the local file system by using cordovafile,
http://ngcordova.com/docs/plugins/file/
And remember all of the files need to be saved as plain text, that means strings, just use JSON.stringify and JSON.parse to make the transform.

1 Like