本地存储设置数组离子列表中的对象



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

经理.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);
}

播放列表.html

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

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

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

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

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

最新更新