主播确认服务多次通话

时间:2018-12-19 22:40:02

标签: javascript angular angular6 primeng

我正在将PrimeNg与Angular 6结合使用,以生成一个确认框,用于从表单中删除项目并保存对表单所做的所有更改。何时

delete() {
  this._confirmationService.confirm({
     message: 'Delete Item?',
     key: 'delete',
     accept: () => {
       // code to delete row
     }
  });
}

submit() {
  this._confirmationService.confirm({
     message: 'Save Changes',
     key: 'submit',
     accept: () => {
       // code to save changes
     }
  });
}

html

<button pButton (click)="delete()"></button>
<button pButton (click)="submit()"></button>

<p-confirmDialog key="delete"></p-confirmDialog>
<p-confirmDialog key="submit"></p-confirmDialog>

不使用键时,两个按钮都调用提交确认功能。在使用键时,“提交”按钮调用提交确认,但在被接受时陷入循环,而删除功能则调用提交确认,如果被拒绝,则调用删除确认。

我需要做什么,以便仅调用特定于该功能的确认服务?

3 个答案:

答案 0 :(得分:1)

尝试此代码:

HTML:

<button type="button" (click)="delete()" pButton icon="pi pi-check" label="Delete"> 
</button>

<button type="button" (click)="submit()" pButton icon="pi pi-times" label="Submit"> 
</button>

<p-confirmDialog ></p-confirmDialog>

TS:

submit() {
    this.confirmationService.confirm({
        message: 'Are you sure that you want to proceed?',
        header: 'Confirmation',
        icon: 'pi pi-exclamation-triangle',
        accept: () => {
            //message here
        },
        reject: () => {
            //message here
        }
    });
}

delete() {
    this.confirmationService.confirm({
        message: 'Do you want to delete this record?',
        header: 'Delete Confirmation',
        icon: 'pi pi-info-circle',
        accept: () => {
            //message here
        },
        reject: () => {
            //message here
        }
    });
}

答案 1 :(得分:0)

您应该将按钮的类型定义为“按钮”,这将使您的浏览器无法选择类型(IE通常选择按钮的类型,而其他人则选择提交)。我还建议重命名您的提交功能,因为命名此提交可能会覆盖与提交按钮相关联的默认提交事件。

答案 2 :(得分:0)

在拒绝确认对话框时,我遇到了类似的问题,我必须单击10次才能退出该对话框。 事实证明,我只是在拒绝事件上错过了confirmationService.close()

confirmDelete(index: number): void {

    this.confirmationService.confirm({
      message: 'Are you sure you want to delete the parameter?',
      accept: () => {
        this.deleteParameter(index);
      },
      reject: () => {
        this.confirmationService.close();
      }
    });
}