How to fetch data using *ngFor directive from storage on Ionic 3 (Cordova, Ionic 3, Angular 5)

jamesharvey

I'm using Ionic's storage to save javascript data object which has several attributes. I'm trying to create a favorite list which can fetch data from ionic storage.

Here's my data provider TS file.

  private items: any[] = [

    {
   "name": "item 01",
   "description": "this is item 01",
   "id": "1"
   },
    {
   "name": "item 02",
   "description": "this is item 02",
   "id": "2"
   },
    {
   "name": "item 03",
   "description": "this is item 03",
   "id": "3"
   },
   {
"name": "item 04",
 "description":"this is item 04",
 "id":"4"
 }
]

and I'm saving items on my html file using a button. the main HTML file is using *ngFor let of directive to fetch the items from the provider.

Main HTML:

<div *ngFor="let item of items"> 
  <h2>{{item.name}}</h2>  
  <p>{{item.description}}</p>
  <button (click)="saveToFav(item)">Save to favorites</button>
</div>

Main TS file:

savToFav(item) {
this.storage.set(this.item.id, this.item);
}

This saves the item with its attributes to Ionic storage. I can see that showing up on my browser's inspect -> application page.

and I'm trying to fetch items from ionic storage to a favorite HTML page.

Favorite HTML page:

  <div *ngFor="let item of items"> 
      <h2>{{item.name}}</h2>  
      <p>{{item.description}}</p>
      <button (click)="remove(item)">Remove from favorites</button>
    </div>

Favorite TS file

    this.platform.ready().then(() => {
    this.storage.get(this.item);
});

But this really doesn't load anything on favorite html page..

What should I do to fetch every item stored in Ionic storage to favorite HTML page?

Thanks in advance,

Taylor Rahul

You are using storage get and set function in wrong manner, All the favorite items must be stored in the single key so that later when required you can have all the list of the favorites. It should be like the below one

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;
})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I load all data from SQlite database? (Ionic 3, Angular 5, Cordova)

From Dev

dynamic key value for data in sqlite storage (Ionic 3, Cordova, Angular 5)

From Dev

How to save a data array object to ionic SQlite storage (TypeScript, Angular 5, Ionic 3)

From Dev

How to add a local cordova plugin with ionic / ionic 2 / ionic 3 / ionic 4 / ionic 5?

From Dev

How to send data via BLE using ionic 3 and angular 4?

From Dev

Ionic3 - "data is undefined" #Fetch #Http

From Dev

Sync data between ionic3 and Angular5 web application using MangoDB

From Dev

Angular Ionic Cordova - Default Image Source Directive

From Dev

Using Ionic Material with Ionic and Cordova 5

From Java

How to fix member Event from @ionic/angular error in Ionic 5

From Dev

arrays in Ionic 3 local storage

From Dev

ionic3 - how can i remove the text in the ngFor component

From Dev

How to $apply and $digest in Angular 4 and IONIC 3?

From Dev

How Integrate Ionic 3 and Angular Material?

From Dev

Using Http to Fetch Remote Data from a Server in Ionic 2

From Dev

How to display json data in ionic 3

From Dev

How to upload images from Ionic 3?

From Dev

Ionic 3 Directive not working with ionic2-autosize

From Dev

Ionic3 - How to implement a persistent chat system with Storage provider?

From Dev

Create Database in SQLite with IONIC 3 + CORDOVA

From Dev

How to add class to the element using Angular's ngFocus directive (or any directive embedded in Ionic Framework) at input in a template?

From Dev

How to use cordova with angular 2 without Ionic

From Dev

Error while updating records in Mongo DB from Ionic 2/ Angular 3 using Node JS

From Dev

Using Bing maps with Ionic 3

From Dev

How to perform error handling from multiple Observables in Angular 2 / Ionic 3?

From Dev

Ionic/Angular custom directive and ionic list thumbnail

From Dev

Get data from link in Ionic 3 Web App

From Dev

Get data from "remote" json to current json ionic 3

From Dev

ionic 3 - Extract data from nested JSON into an array

Related Related

  1. 1

    How can I load all data from SQlite database? (Ionic 3, Angular 5, Cordova)

  2. 2

    dynamic key value for data in sqlite storage (Ionic 3, Cordova, Angular 5)

  3. 3

    How to save a data array object to ionic SQlite storage (TypeScript, Angular 5, Ionic 3)

  4. 4

    How to add a local cordova plugin with ionic / ionic 2 / ionic 3 / ionic 4 / ionic 5?

  5. 5

    How to send data via BLE using ionic 3 and angular 4?

  6. 6

    Ionic3 - "data is undefined" #Fetch #Http

  7. 7

    Sync data between ionic3 and Angular5 web application using MangoDB

  8. 8

    Angular Ionic Cordova - Default Image Source Directive

  9. 9

    Using Ionic Material with Ionic and Cordova 5

  10. 10

    How to fix member Event from @ionic/angular error in Ionic 5

  11. 11

    arrays in Ionic 3 local storage

  12. 12

    ionic3 - how can i remove the text in the ngFor component

  13. 13

    How to $apply and $digest in Angular 4 and IONIC 3?

  14. 14

    How Integrate Ionic 3 and Angular Material?

  15. 15

    Using Http to Fetch Remote Data from a Server in Ionic 2

  16. 16

    How to display json data in ionic 3

  17. 17

    How to upload images from Ionic 3?

  18. 18

    Ionic 3 Directive not working with ionic2-autosize

  19. 19

    Ionic3 - How to implement a persistent chat system with Storage provider?

  20. 20

    Create Database in SQLite with IONIC 3 + CORDOVA

  21. 21

    How to add class to the element using Angular's ngFocus directive (or any directive embedded in Ionic Framework) at input in a template?

  22. 22

    How to use cordova with angular 2 without Ionic

  23. 23

    Error while updating records in Mongo DB from Ionic 2/ Angular 3 using Node JS

  24. 24

    Using Bing maps with Ionic 3

  25. 25

    How to perform error handling from multiple Observables in Angular 2 / Ionic 3?

  26. 26

    Ionic/Angular custom directive and ionic list thumbnail

  27. 27

    Get data from link in Ionic 3 Web App

  28. 28

    Get data from "remote" json to current json ionic 3

  29. 29

    ionic 3 - Extract data from nested JSON into an array

HotTag

Archive