javascript - LocalStorage Setting Object Inside Array Ionic List - Stack Overflow

I am trying to use LocalStorage to store an array that contains objects. Right now the following code i

I am trying to use LocalStorage to store an array that contains objects. Right now the following code is returning an object on the console and not returning as an array. This means that my ion-list cannot read it. Is there any way around this and getting back the value as an array with my objects in the array? The object presentation contains multiple things such as ID, title, etc etc. I want to be able to store multiple presentations in the array and be able to access each one and display them in the ion list.

Manager.js

 playlistService.addPlaylistAll = function (presentation) {

      console.log("setting item");
        var playlistarraytest = [];
      playlistarraytest.push(presentation);
      console.log("array first!! ", playlistarraytest);
      localStorage.setItem('playlisttest', playlistarraytest);
        playlistService.refresh();
      var test = localStorage.getItem('playlisttest');
       console.log(test);
}

Playlist.html

 <ion-list ng-repeat="presentation in dayOne = (playlist | filter: { day: 1 } | orderBy: 'start_date')">

I am trying to use LocalStorage to store an array that contains objects. Right now the following code is returning an object on the console and not returning as an array. This means that my ion-list cannot read it. Is there any way around this and getting back the value as an array with my objects in the array? The object presentation contains multiple things such as ID, title, etc etc. I want to be able to store multiple presentations in the array and be able to access each one and display them in the ion list.

Manager.js

 playlistService.addPlaylistAll = function (presentation) {

      console.log("setting item");
        var playlistarraytest = [];
      playlistarraytest.push(presentation);
      console.log("array first!! ", playlistarraytest);
      localStorage.setItem('playlisttest', playlistarraytest);
        playlistService.refresh();
      var test = localStorage.getItem('playlisttest');
       console.log(test);
}

Playlist.html

 <ion-list ng-repeat="presentation in dayOne = (playlist | filter: { day: 1 } | orderBy: 'start_date')">
Share Improve this question asked May 15, 2018 at 15:04 Curtis BoylanCurtis Boylan 8271 gold badge10 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You cannot store data structures directly in LocalStorage. LocalStorage only stores strings. So you must use:

let json = JSON.stringify(playlistarraytest);
localStorage.setItem('playlisttest', json);

And then to retrieve it use:

var test = localStorage.getItem('playlisttest');
let arr = JSON.parse(test);
console.log(arr);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745351717a4623872.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信