#angularjs #typescript
#angularjs #машинописный текст
Вопрос:
Я пытаюсь сохранить товары в корзине в БД, передав выбранные товары в функцию отправки, но я не могу извлечь выбранный товар и передать его в onSubmit(товар).
Ниже приведен фрагмент Ts и html-кода, над которыми я работал, я хочу, чтобы можно было передавать товары из корзины в список базы данных-items.component.ts
import { Component, OnInit } from '@angular/core'; import { ItemService } from '../../services/item.service'; import { Router } from '@angular/router'; import { OMService } from 'src/app/services/OM.service'; import { FormBuilder } from '@angular/forms'; import { ActivatedRoute, ParamMap } from '@angular/router'; @Component({ selector: 'app-list-items', templateUrl: './list-items.component.html', styleUrls: ['./list-items.component.css'] }) export class ListItemsComponent implements OnInit { public mode = 'Add'; //default mode constructor(private fb: FormBuilder, private _myService: ItemService,private _myOMService: OMService, private router:Router, public route: ActivatedRoute) { } public items : any = []; public grandTotal !: number; //constructor(private cartService : CartService) { } removeItem(item: any){ this._myOMService.removeCartItem(item); } emptycart(){ this._myOMService.removeAllCart(); } ngOnInit() { //this.getCartItems(); this._myOMService.getProducts() .subscribe(res=gt;{ this.items = res; this.grandTotal = this._myOMService.getTotalPrice(); }) } onDelete(itemId: string) { this._myOMService.deleteCartItems(itemId); this.router.navigate(['/listCartItems']); } update(){ this.router.navigate(['/addItems/']); } onSubmit(item : any) { let cartId =1; console.log("Item : ", item) this.router.navigate(['/addPayment/' cartId]); } }
list-items.component.html
lt;ng-container *ngIf = "true"gt; lt;div class="container"gt; lt;div class="card-table"gt; lt;div class="cart-product"gt; lt;table class="table table-responsive"gt; lt;theadgt; lt;trgt; lt;thgt;Sr.Nolt;/thgt; lt;thgt;Product Namelt;/thgt; lt;thgt;Product Imagelt;/thgt; lt;thgt;Descriptionlt;/thgt; lt;thgt;Pricelt;/thgt; lt;thgt;Quantitylt;/thgt; lt;!-- lt;thgt;Totallt;/thgt; --gt; lt;thgt;Actionlt;/thgt; lt;/trgt; lt;/theadgt; lt;tbodygt; lt;tr *ngFor ="let item of items let i = index"gt; lt;tdgt;{{i 1}}lt;/tdgt; lt;tdgt;{{item.title}}lt;/tdgt; lt;tdgt;lt;img style="width: 120px;" src="{{item.image}}" alt=""gt;lt;/tdgt; lt;td style="width: 25%;"gt;{{item.description}}lt;/tdgt; lt;th style="width: 12%;"gt;{{item.price}}lt;/thgt; lt;td style="width: 12%;"gt;{{item.quantity}}lt;/tdgt; lt;!-- lt;td style="width: 12%;"gt;{{item.total}}lt;/tdgt; --gt; lt;tdgt; lt;button (click)="removeItem(item)"class="btn btn-danger"gt;lt;i class="fas fa-trash-alt"gt;lt;/igt;lt;/buttongt; lt;button (click)="update()" class="btn btn-primary"gt;Updatelt;/buttongt; lt;/tdgt; lt;/trgt; lt;trgt; lt;td colspan="4"gt;lt;/tdgt; lt;tdgt; Total : ${{grandTotal}} lt;/tdgt; lt;/trgt; lt;trgt; lt;td colspan="4"gt;lt;/tdgt; lt;tdgt;lt;button (click)="emptycart()" class="btn btn-danger"gt;Empty Cartlt;/buttongt;lt;/tdgt; lt;tdgt;lt;button (click)="onSubmit()" class="btn btn-success"gt;Checkoutlt;/buttongt;lt;/tdgt; lt;!-- lt;tdgt;lt;stronggt;Grand Total : ${{grandTotal}}lt;/stronggt;lt;/tdgt; --gt; lt;/trgt; lt;/tbodygt; lt;/tablegt; lt;/divgt; lt;/divgt; lt;/divgt;