最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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);
发布评论

评论列表(0)

  1. 暂无评论