如何使用*ngFor指令从Ionic 3(Cordova、Ionic 3、Angular 5)上的存储器中获取数据

如何使用*ngFor指令从Ionic 3(Cordova、Ionic 3、Angular 5)上的存储器中获取数据,angular,cordova,ionic-framework,ionic2,ionic3,Angular,Cordova,Ionic Framework,Ionic2,Ionic3,我正在使用Ionic的存储来保存javascript数据对象,该对象有几个属性。我正在尝试创建一个收藏夹列表,它可以从离子存储中获取数据 这是我的数据提供者TS文件 我用一个按钮保存html文件中的项目。 主HTML文件使用*ngFor let of指令从提供程序获取项目 主HTML: 这会将项目及其属性保存到Ionic storage。我可以在浏览器的inspect->application页面上看到它 我正在尝试从ionic存储中获取项目到最喜欢的HTML页面 最喜爱的HTML页面: 但这实

我正在使用Ionic的存储来保存javascript数据对象,该对象有几个属性。我正在尝试创建一个收藏夹列表,它可以从离子存储中获取数据

这是我的数据提供者TS文件

我用一个按钮保存html文件中的项目。 主HTML文件使用*ngFor let of指令从提供程序获取项目

主HTML:

这会将项目及其属性保存到Ionic storage。我可以在浏览器的inspect->application页面上看到它

我正在尝试从ionic存储中获取项目到最喜欢的HTML页面

最喜爱的HTML页面:

但这实际上不会在最喜欢的html页面上加载任何内容

我应该怎么做才能将存储在Ionic storage中的每个项目提取到最喜欢的HTML页面


提前感谢,

您以错误的方式使用了存储获取和设置功能,所有收藏项目必须存储在单键中,以便以后需要时可以获得所有收藏列表。它应该像下面的一样

savToFav(item) {
  this.storage.get('favoritesList')
  .then((fav)=>{
    if(fav == null){
       fav = [];
    }
    //This will fetch the old items and push the new item in array
    fav.push(item); return fav;
  })
  .then((fav)=>{
    //this will store the new update favorite list array in storage.
    this.storage.set('favoritesList',fav);
  })
}

//Favorite ts file you can use it like below 
this.storage.get('favoritesList').then((fav)=>{
 //you can asssing it any variable and use in your *ngFor loop
  this.myFavList = fav;
})

@侯赛因:如果OP使用的是数据集,则获得返回承诺。您可能想修复您的问题answer@Hussein,谢谢你的回答。。但这个问题有点不同。我正试图取回所有物品。现在,我可以使用上面的示例保存项目,但我正在尝试从ionic storage中提取所有项目,并使用*ngFor指令调用它们。您能告诉我加载项目的组件吗?可能是重复的,谢谢您的回复!如何将其分配给这个.storage.get'favoritesList'中的变量。然后FAV=>{}?我正在尝试此操作。push会抛出一条错误消息。它表示“无法读取null TypeScript error的属性‘push’”,您可以添加对此类属性的检查case@jamesharvey我已经编辑了答案以添加空值检查谢谢!我通过使用接口和数组解决了这个问题!
<div *ngFor="let item of items"> 
  <h2>{{item.name}}</h2>  
  <p>{{item.description}}</p>
  <button (click)="saveToFav(item)">Save to favorites</button>
</div>
savToFav(item) {
this.storage.set(this.item.id, this.item);
}
  <div *ngFor="let item of items"> 
      <h2>{{item.name}}</h2>  
      <p>{{item.description}}</p>
      <button (click)="remove(item)">Remove from favorites</button>
    </div>
    this.platform.ready().then(() => {
    this.storage.get(this.item);
});
savToFav(item) {
  this.storage.get('favoritesList')
  .then((fav)=>{
    if(fav == null){
       fav = [];
    }
    //This will fetch the old items and push the new item in array
    fav.push(item); return fav;
  })
  .then((fav)=>{
    //this will store the new update favorite list array in storage.
    this.storage.set('favoritesList',fav);
  })
}

//Favorite ts file you can use it like below 
this.storage.get('favoritesList').then((fav)=>{
 //you can asssing it any variable and use in your *ngFor loop
  this.myFavList = fav;
})