在这tutorial之后,我创建了一个可重用和可定制的模式组件.然而,我有一个问题. 假设有两个组件,A和B,在这两个组件中,我都使用modal组件打开了一个模型.对于组件A,当用户单击提交按钮时,必须执行组件A的函数A(),而对于组件B,则必须执行组件B的函数B().如何使模式.ts文件能够访问并执行函数A()和函数B()?
如果可能的话,我不想有一个全局服务文件,所有函数都在那里,或者将所有相关函数移动到modal.ts文件,相反,我喜欢保持每个单独的组件干净和独立,但优化它们之间的交互,这样我就只传递函数的名称和所需的输入参数.有人知道实现这一目标的最佳做法是什么吗?先谢谢你了.
模式的.TS:
open(): Promise<boolean> {
...
}
async close(): Promise<void> {
const result = await this.modalConfig.onClose();
this.modalRef.close(result);
}
async dismiss(): Promise<void> {
...
}
}
config模式的.TS:
export class ModalConfig {
modalTitle: string;
data: string = '';
onClose?(): Promise<boolean> | boolean;
}
modal的.html:
<ng-template #modal>
<div class="modal-header">
<button type="button" (click)="dismiss()">cancel</button>
<button type="button" (click)="close()">Ok</button>
</div>
</ng-template>