使用 Angular 删除 table 上的所有零数量后,如何提醒成功消息?

How can I alert the success message after I deleted all zero quantity on table using Angular?

当我点击删除按钮时,它会删除所有数量为零的产品。如果 table 的数量为零,我会添加一条警告消息来通知用户。成功消息将在删除所有数量为零的产品后提示,如果 table 没有数量为零则错误消息提示。我正在使用 sweetalert2。问题是当我删除所有数量为零的产品时,显示的是错误消息而不是成功消息。我想在删除产品后显示成功消息,错误消息仅在 table 没有零数量时显示。删除所有数量为零的商品后如何提示成功?

//删除所有数量为零的行

public onDeleteProduct(): void {
    this.productServive.deleteProduct().subscribe(
      (response: void) => {
        console.log(response);
        this.messageShow();
        this.getAllProduct();
        this.onCloseUpdateHandled();
      },
      (error: HttpErrorResponse) => {
        this.errorMessage(error.message);
      }
    );
  }

//产品数量是否为零

public messageShow(): void {
    for(const product of this.products) {
      if(product.quantity === 0) {
        this.successMessage("deleted");
      } else {
        this.errorMessage("No more out-of-stock");
      }
    }
  }

//html

<button type="button" class="btn btn-outline-danger btn-sm" (click)="onDeleteProduct()">Delete</button>

//例子table enter image description here

只有当您确认没有产品的数量为 0 时,您才能打印错误消息。因此它将是:

public messageShow(): void {
    for(const product of this.products) {
      if(product.quantity === 0) {
        this.successMessage("deleted");
        return;
      }
    }
    this.errorMessage("No more out-of-stock");
}

此方法遍历列表中的所有产品。如果它找到一个数量为 0 的,它会显示成功消息并结束该方法(这就是 void return 的用途)。如果它遍历了所有产品,但没有找到任何数量为 0 的产品,则会显示错误消息