如何在 Angular 中仅显示一个针对多个删除的警报?

问题描述 投票:0回答:1

我有这样的代码=>

return next.handle(req).pipe(
  map((response: any) => {
    if ( req.method !== "GET" && response instanceof HttpResponse && response.status === 200) {
      this.alertService.showSuccessAlert();
    }
  return response;
}),

我想在多次删除中只显示一个警报。在当前场景中,页面上每个已删除的项目都会显示一个警报,因此当删除 10 个项目时,这意味着会出现混乱。

我尝试通过创建布尔变量来解决,但没有成功。

编辑: 我的组件=>

multipleDeleteCategory() { 
  const selectedCategories = this.myForm.value.selectedCategory;
   if (selectedCategories && selectedCategories.length > 0) { 
    selectedCategories.forEach((category: Category) => { 
      this.categoryService.deleteCategory(category.id).subscribe(() => { 
        this.categories = this.categories.filter(cat => cat.id !== category.id); 
      }); 
    }); 
  } 
}

警报服务=>

public showSuccessAlert(): void { 
  this.toastrService.success('Your transaction has been completed', 'Success:', this.getAlert()); }
javascript angular typescript error-handling interceptor
1个回答
0
投票

我认为你需要使用这个方法

findDuplicate
来检查消息是否已经存在,如果不存在,则显示弹出窗口!

你编码

public showSuccessAlert(): void { 
    if(!this.toastrService.findDuplicate('Your transaction has been completed', 'Success:')) {
        this.toastrService.success('Your transaction has been completed', 'Success:', this.getAlert()); 
    }
}

查找重复源代码

/**
   * Determines if toast message is already shown
   */
  findDuplicate(title = '', message = '', resetOnDuplicate: boolean, countDuplicates: boolean) {
    const { includeTitleDuplicates } = this.toastrConfig;

    for (const toast of this.toasts) {
      const hasDuplicateTitle = includeTitleDuplicates && toast.title === title;
      if ((!includeTitleDuplicates || hasDuplicateTitle) && toast.message === message) {
        toast.toastRef.onDuplicate(resetOnDuplicate, countDuplicates);
        return toast;
      }
    }

    return null;
  }
© www.soinside.com 2019 - 2024. All rights reserved.