我有一个使用确认服务的组件,如下所示
@Component({
moduleId: module.id,
templateUrl: 'account-list.component.html',
providers: [AccountService],
selector: 'account-list'
}).....
deleteAccount(Account: Account) {
this.confirmationService.confirm({
header: 'Delete Confirmation', message: 'Are you sure that you want to delete this account?',
icon: 'fa fa-trash',
accept: () => {
this.AccountService.deleteAccount(Account.account_id).subscribe(
res => {
let index: number = this.Accounts.findIndex(n => n.account_id === Account.mc_account_id);
if (~index) {
this.Accounts.splice(index, 1);
}
this.msgs.push({ severity: 'Info', summary: 'The account is deleted' });
},
err => {
this.msgs.push({ severity: 'Error', summary: err.message });
}
);
}
});
}
Run Code Online (Sandbox Code Playgroud)
这个组件可以单独使用.将此组件用作子组件时,将触发两次确认服务.即当我想删除一个对象时,我需要多次点击接受或拒绝按钮.
我在数据表组件的rowexpantion中使用此组件,如下所示:
<template let-parent pTemplate="rowexpansion">
<div class="outer" *ngIf="parent">
<div class="inner">
<account-list [parentId]="parent.parent_id"> </account-list>
</div>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
在此示例中,我的组件用于显示父组件的Datatable的帐户详细信息,每次展开父表的一行时,我需要再次单击"接受"或"拒绝"按钮.问候
基于primeng Docs
key:匹配确认对象的键的可选键,当组件树具有多个确认对话框时使用.
由于我使用的是嵌套组件树,我需要在html模板中为每个确认服务添加一个密钥,如下所示
<p-confirmDialog key="account"></p-confirmDialog>
Run Code Online (Sandbox Code Playgroud)
在ts代码中我还需要指定密钥如下:
this.confirmationService.confirm({
header: 'Delete Confirmation',
key:'account';
Run Code Online (Sandbox Code Playgroud)