简体   繁体   English

ngx 模态未在 angular 中关闭

[英]ngx modal not closing in angular

i have one modal called from another component, but when i press the X button to close it doesnt work:我有一个从另一个组件调用的模式,但是当我按下 X 按钮关闭它时它不起作用:

This is the button where i am calling the modal:这是我调用模态的按钮:

<button type="submit" id="submit-form" class="btn btn-primary" (click)="openConfirmModal()">Restaurar</button>

As you can see, i am calling the method openConfirmModal() from my actual component, which is calling the modal from another service component called password-reset.service.ts :如您所见,我正在从我的实际组件调用方法openConfirmModal() ,该组件从另一个名为password-reset.service.ts服务组件调用模式:

import { PasswordResetService } from './password-reset.service';
export class PasswordResetComponent implements OnInit {
  modalRef: BsModalRef;
  
  constructor(private modalService: BsModalService,
   private passwordResetService: PasswordResetService) { }

  openConfirmModal() {
    this.passwordResetService.confirmar("HECHO", "Datos verificados! Por favor, revise su bandeja de entrada...").subscribe((answer) => {});
  }

This is my password-service.ts :这是我的password-service.ts

import { ConfirmModalComponent } from './modals/confirm-modal/confirm-modal.component';
export class PasswordResetService {
  bsModalRef: BsModalRef;

  constructor(private bsModalService: BsModalService) { }

  public confirmar(title: string, message: string) : Observable<string> {
    const initialState = {
      title,
      message,
    };
    this.bsModalRef = this.bsModalService.show(ConfirmModalComponent, { initialState });
  
    return new Observable<string>(this.getModalSubscriber());
  }

  private getModalSubscriber() {
    return (observer) => {
        const subscription = this.bsModalService.onHidden.subscribe((reason: string) => {
            observer.complete()
        });

        return {
            unsubscribe() {
                subscription.unsubscribe();
            }
        }
    }
  }
}

Finally, this is the component who have the modal and where i am trying to call:最后,这是具有模态的组件以及我试图调用的位置:

confirm-modal.component.html:确认-modal.component.html:

<div class="modal-header">
    <h4 class="modal-title">{{title}}</h4>
    <button type="button" class="close pull-right" aria-label="Close" (click)="onClose()">
        <span aria-hidden="true">&times;</span>
    </button>
</div>
<div class="modal-body">
    <div class="message-margin">
      <p>{{message}}</p>
    </div>
</div>

confirm-modal.component:确认模态组件:

export class ConfirmModalComponent implements OnInit {

  title: string;
  message: string;
  modalRef: BsModalRef;

  constructor(private modalService: BsModalService) { 
  }

  ngOnInit() {
  }

  onClose() {
    this.modalRef.hide();
  }
}

Anyone can help me with this?任何人都可以帮助我吗? thank you..谢谢你..

You can send/listen for a close event in your getModalSubscriber method.您可以在 getModalSubscriber 方法中发送/收听关闭事件。 then you can call this.modalRef.hide() when the close event is captured.然后您可以在捕获关闭事件时调用 this.modalRef.hide() 。

Give click to close modal event to span rather than button给点击关闭模式事件跨越而不是按钮

<div class="modal-header">
    <h4 class="modal-title">{{title}}</h4>
    <button type="button" class="close pull-right" aria-label="Close">
        <span aria-hidden="true" (click)="onClose()">&times;</span>
    </button> </div> <div class="modal-body">
    <div class="message-margin">
      <p>{{message}}</p>
    </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM