My Aim is
- Create a shopping list which is pulling recipes from an API.
- Take ingredients from one page to another.
- Page to refresh/load data when more than 1 is added.
The issue I am having is
- Only 1 set of ingredients load
- The clear function will not allow anymore to be added.
Recipe Page
// Loading Recipes ///////////////////////////////////////////////////////////////////// loadDetails1(id){ this.apiAuthentication.loadDetails(id) .then(data => { this.api = data; }); } // Add to Shopping List ///////////////////////////////////////////////////////////////////// submit(api) { let toast = this.toastCtrl.create({ message: 'Added to shopping list', duration: 1000 }); console.log(this.api); this.storage.get('myData').then((api) => { // add one igredient to the ingredientLines object // if it's still a string use JSON.parse() on it this.storage.set('myData', api).then(result =>{ toast.present(); console.log(api); }); }); }
HTML
<h1 (click)="submit(api?.ingredientLines)">Add to shopping list</h1> <ul> <li *ngFor="let item of api?.ingredientLines"><span>{{item}}</span></li> </ul>
Shopping List Page
getData() { this.storage.get('myData').then((data => { this.api = data; console.log(data); setInterval(() => { console.log(data); },5000); })); }
HTML
<ion-content padding> <ion-card> <ion-card-header> {{api?.name}} </ion-card-header> <ion-list> <ion-item> <ul> <li *ngFor="let item of api?.ingredientLines"> <ion-label>{{item}}</ion-label> <ion-checkbox></ion-checkbox> </li> </ul> </ion-item> </ion-list> <button ion-button block full color="danger" (click)="clear(item)">Remove</button> </ion-card> </ion-content>
Error shown in visual is https://www.youtube.com/watch?v=BDS_XTdw2S0 You can see that when I add an item to the shopping list it does not update until i close the app and restart it. Also there is only ever 1 item.
1 Answers
Answers 1
You're overwriting your storage that's why only 1 appears.
It should look like this, try to use types and promises
public submit(ingredientLines: any) { let toast = this.toastCtrl.create({ message: 'Added to shopping list', duration: 1000 }); this.storage.get('myData').then((ingredientLines) => { console.log(ingredientLines); // add one igredient to the ingredientLines object // if it's still a string use JSON.parse() on it this.storage.set('myData', ingredientLines).then(result =>{ toast.present(); }); }); }
So
Get the data from storage
Apply new item to data
Set new data to storage
0 comments:
Post a Comment