简体   繁体   English

从购物车中删除按钮不起作用,在 angular

[英]remove from cart button not working, in angular

I'm trying to hit the remove button and for my html quantity to update to one quantity lower for that item only, then remove it entirely when the quantity in 0.but when i hit the remove button nothing happens.我正在尝试点击删除按钮,并将我的 html 数量更新为仅针对该项目降低一个数量,然后在数量为 0 时将其完全删除。但是当我点击删除按钮时没有任何反应。 any help is appreciated.任何帮助表示赞赏。 Also my commented out foreach loop works at changing the html but changes the quantity for all products when i hit the remove button......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................此外,我注释掉的 foreach 循环可用于更改 html,但当我点击删除按钮时会更改所有产品的数量.......................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ………… ................................................................................................................................................................................................................................. ..................................................... ..................................................... ..................................................... ..................................................... …………………………………………………………………………………………………………

```ts
import {Component, Input, OnInit} from '@angular/core';
import { Router } from '@angular/router';
import { Product } from 'src/app/models/product';
import { ProductService } from 'src/app/services/product.service';

@Component({
  selector: 'app-cart',
  templateUrl: './cart.component.html',
  styleUrls: ['./cart.component.css']
})
export class CartComponent implements OnInit {

  //added
  cartCount!: number;

  products: {
    product: Product,
    quantity: number,

  }[] = [];
  totalPrice!: number;
  cartProducts: Product[] = [];

  //added
  @Input() productInfo!: Product;

  constructor(private productService: ProductService, private router: Router) { }

  ngOnInit(): void {
    this.productService.getCart().subscribe(
      (cart) => {
        this.products = cart.products;
        this.products.forEach(
          (element) => this.cartProducts.push(element.product)
        );
        this.totalPrice = cart.totalPrice;
      }
    );
  }

  emptyCart(): void {
    let cart = {
      cartCount: 0,
      products: [],
      totalPrice: 0.00
    };
    this.productService.setCart(cart);
    this.router.navigate(['/home']);
  }

  //added
  removeFromCart(product: Product): void {

    /*this.products.forEach(
      (element) => {
        --element.quantity;
        if(element.quantity<1){
          this.products.pop();
        }
      }
    );*/
    --product.quantity;
    if(product.quantity<1){
      this.products = this.products.filter(({product: p}) => p.id !== product.id)
    }
  }



}```





```html
<app-navbar></app-navbar>
<table class="table table-striped" id="cart-table">
    <thead>
      <tr>
        <th scope="col">ID</th>
        <th scope="col">Name</th>
        <th scope="col">Quantity</th>
        <th scope="col">Description</th>
      </tr>
    </thead>
    <tbody id="cart-table-body">
        <tr *ngFor="let product of products;">
            <td>{{product.product.id}}</td>
            <td>{{product.product.name}}</td>
            <td>{{product.quantity}}</td>
            <td>{{product.product.description}}</td>
          //added
          <td><button class="btn btn-outline-success" (click)="removeFromCart(productInfo)">Remove from Cart</button></td>
        </tr>
    </tbody>
</table>
<button class="btn btn-danger" [routerLink]="['/home']">Back</button>
<button class="btn btn-warning" (click)="emptyCart()">Empty Cart</button>
<button class="btn btn-primary" [routerLink]="['/checkout']">Proceed to Checkout (${{totalPrice}})</button>
```

[picture of my html][1]


  [1]: https://i.stack.imgur.com/b3W3I.png

this.products.filter(({product: p}) => p.id.== product.id) should be this.products.filter(({product: p}) => p.id.== product.id)应该是
this.products.filter((p:Product) => p.id.== product.id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM