阵列离子列表中的LocalStorage设置对象

Cur*_*lan 0 javascript arrays local-storage angularjs ionic-framework

我正在尝试使用LocalStorage存储包含对象的数组。现在,以下代码在控制台上返回一个对象,而不是作为数组返回。这意味着我的离子列表无法阅读。有什么办法可以解决这个问题,并且可以将数组中的对象作为数组返回值?对象演示文稿包含多个内容,例如ID,标题等。我希望能够在数组中存储多个演示文稿,并能够访问每个演示文稿并在离子列表中显示它们。

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);
}
Run Code Online (Sandbox Code Playgroud)

Playlist.html

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

Ran*_*urn 5

您不能直接在LocalStorage中存储数据结构。LocalStorage仅存储字符串。因此,您必须使用:

let json = JSON.stringify(playlistarraytest);
localStorage.setItem('playlisttest', json);
Run Code Online (Sandbox Code Playgroud)

然后使用以下命令检索它:

var test = localStorage.getItem('playlisttest');
let arr = JSON.parse(test);
console.log(arr);
Run Code Online (Sandbox Code Playgroud)