[ng-bootstrap]:将函数传递给模态引发了一个服务



我已经创建了一个调用我的不同模态的服务。所以我有一个模态调用"Confirm modal",它可以接受3个参数,一个确认函数,一个拒绝函数和一个主体。以下是模式:

export class ConfirmModalComponent implements OnInit {
@Output() acceptFunc;
@Output() refuseFunc?;
@Input() body: string;
constructor(public activeModal: NgbActiveModal) {}
ngOnInit() {}
async confirmModal() {
await this.acceptFunc;
this.closeModal();
}
async refuseModal() {
if (this.refuseFunc) await this.refuseFunc();
this.closeModal();
}
closeModal() {
this.activeModal.close('Modal Closed');
}
}

在我的模式服务中,我创建了以下函数来打开这个模式

openConfirmModal(accept: <P = any>(props?: P) => void, body: string,         refuse?: <P = any>(props?: P) => void): NgbModalRef {
const modalRef = this.modalService.open(ConfirmModalComponent, { size: 'lg' });
modalRef.componentInstance.accept = accept;
if (refuse) modalRef.componentInstance.refuse = refuse;
modalRef.componentInstance.body = body;
return modalRef;
}

`

我调用这个函数如下:

openConfirmModal() {
this.modalService.openConfirmModal(this.myFunc.bind(this), 'Test')
}
myFunc() {
console.log('Work !');
}

问题是我的函数myFunc从来没有被调用过,所以我如何通过服务将函数从组件传递到我的模态?

在ConfirmModalComponent上,您没有调用带括号的方法,请更改

async confirmModal() {
await this.acceptFunc;
this.closeModal();
}

async confirmModal() {
await this.acceptFunc();
this.closeModal();
}

在openConfirmModal方法中,您定义的函数名称与您在组件上使用的名称不同,请更改这两行

modalRef.componentInstance.accept = accept;
if (refuse) modalRef.componentInstance.refuse = refuse;

modalRef.componentInstance.acceptFunc = accept;
if (refuse) modalRef.componentInstance.refuseFunc = refuse;

如果您想调用组件中与modal相关的方法,只需如下调用即可this.modalRef.content.yourfunction((;

相关内容

最新更新