【问题标题】:LocalStorage Setting Object Inside Array Ionic List数组离子列表内的LocalStorage设置对象
【发布时间】:2018-10-25 11:36:56
【问题描述】:

我正在尝试使用 LocalStorage 来存储包含对象的数组。现在,以下代码在控制台上返回一个对象,而不是作为数组返回。这意味着我的 ion-list 无法读取它。有什么办法可以解决这个问题并将值作为数组取回,我的对象在数组中?对象表示包含多个内容,例如 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);
}

播放列表.html

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

【问题讨论】:

    标签: javascript arrays angularjs ionic-framework local-storage


    【解决方案1】:

    您不能将数据结构直接存储在 LocalStorage 中。 LocalStorage 只存储字符串。所以你必须使用:

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

    然后检索它使用:

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

    【讨论】:

      猜你喜欢
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 2021-10-21
      • 2020-05-30
      相关资源
      最近更新 更多