It would appear that the onSubmit function below is saving to localStorage but I deployed the code to the Web and when I refreshed the page, the shopping cart got emptied. I can't test the code after every modification because I'm in development mode and the page re-compiles when it's refreshed but I can test it with the deleteItem function which uses splice. Splice only works on an array and when I click the delete button, the product gets cleared so that tells me it's not using localStorage.
In the component:
- items = [];
- public onSubmit(thumbnail, quantity, product_name, product_price){
- const data = {
- thumbnail,
- quantity,
- product_name,
- product_price
- };
- localStorage.setItem('items', JSON.stringify(data));
- this.items.push(JSON.parse(localStorage.getItem('items')));
- this.isSubmitted = true;
- }
- deleteItem(i){
- this.items.splice(i,1);
- }
In the HTML:
- <tr *ngFor="let item of items; let i = index">
- <td>
- <button type="button" (click)="deleteItem(i)">Xbutton>td>
- <td><img src="{{item.thumbnail}}" />td>
- <td>{{item.quantity}} td>
- <td>{{item.product_name }} td>
- <td>{{item.product_price }} td>
- <td>{{item.quantity * item.product_price }}td>
- tr>


Maureen MooreRank #5,080 · 312 reputationPosted Jul 28, 2020, 2:49 AM EDT
Varun SetiaRank #308 · 8.2K reputationPosted Jul 27, 2020, 7:36 PM EDT
Rajeesh MenothRank #118 · 18.4K reputationPosted Jul 27, 2020, 7:38 PM EDT
Varun SetiaRank #308 · 8.2K reputationPosted Jul 28, 2020, 3:14 AM EDT
Maureen MooreRank #5,080 · 312 reputationPosted Jul 28, 2020, 3:01 PM EDT
Maureen MooreRank #5,080 · 312 reputationPosted Jul 28, 2020, 3:06 PM EDT
Maureen MooreRank #5,080 · 312 reputationPosted Jul 29, 2020, 3:59 AM EDT
Varun SetiaRank #308 · 8.2K reputationPosted Jul 28, 2020, 4:50 AM EDT