I am creating a generic modal component that has the usual attributes to be passed: title, content (using <ng-content></ng-content>
), and customization for buttons.
My question is if I am filling the content of the modal as a form, and this form needs validation before closing the modal, how can we make the validator stop the modal from closing?
I saw in these answers that it is not recommended for passing a function to the component to use @Input
and the better way is using @Output
which actually accesses the parent function instead of passing it. But that does not allow me to stop the modal from closing, the code looks like this now:
in parent.component.html:
<app-modal title="test" (callback)="validator()"> ...form content ...</app-modal>
in modal.component.ts:
@Output() callback: EventEmitter<any> = new EventEmitter();
onConfirm(): void {
this.callback.emit();
// here I should stop the execution in case the validation did not pass
this.onClose.next(1);
this.modalRef.hide(); // using ngx-bootstrap modal
}
I can not get a return value from the callback .
Is there a clean way to achieve this without using @Input for passing functions?