ngx-materialize icon indicating copy to clipboard operation
ngx-materialize copied to clipboard

Method close in MzModalService

Open LucasViniciusPereira opened this issue 7 years ago • 4 comments

Hello, i'm need of method close in MzModalService, because when a component die i need close modal a modal it's open.

exemaple:

constructor(private modalService: MzModalService) { }

 ngOnDestroy() {
    this.modalService.close(ModalExampleComponent);
 }

LucasViniciusPereira avatar Mar 20 '18 14:03 LucasViniciusPereira

Althought this is not ideal, you can still close the modal programmatically when using MzModalService by using the returned ComponentRef<MzBaseModal> object.

private modalExampleRef: ComponentRef<MzBaseModal>;

constructor(private modalService: MzModalService) { }

ngOnDestroy() {
  this.closeModal();
}

openModal() {
  // open modal via MzModalService and get injected modal component reference
  this.modalExampleRef = this.modalService.open(ModalExampleComponent);
}

closeModal() {
  // if modal reference is not undefined/null close it using modal component reference
  if (this.modalExampleRef) {
    this.modalExampleRef.instance.modalComponent.close();
  }
}

jfcere avatar Mar 20 '18 15:03 jfcere

I need too.

Albejr avatar Mar 20 '18 16:03 Albejr

@Albejr for now you can use the workaround that I provided but I'll see what I can do to ease the way to close modal when using MzModalService in a near future.

I'll keep the issue open until then.

jfcere avatar Mar 20 '18 17:03 jfcere

TKS, according Materializecss:

  ngOnDestroy(): void {
    $('#mymodal').modal('close');
  }

ng2-materialize is beautiful.

Albejr avatar Mar 20 '18 17:03 Albejr