Angular(2/4)modalService.close()不起作用

问题描述 投票:2回答:2

这是我的代码:

import { NgbModal } from '@ng-bootstrap/ng-bootstrap';`

    constructor(private http: Http, private route: ActivatedRoute,private router: Router, private modalService: NgbModal) { }

    editDocumentRow = function (documentId, modal) {
        this.http.get(ConstantComponent.url + '/documentmanagerapi/Get/' + documentId).map((res: Response) => res.json()).subscribe(data => {
        this.documentsRowDetails = data
        this.modalService.open(modal)
        this.modalService.close() // not working
    })
  }

我正在尝试在其他功能中使用this.modalService.close()。但是,即使在this.modalService.open(modal)正常工作的同一功能中,它也无法正常工作。

我收到此错误:this.modalService.close is not a function

任何建议,这里出了什么问题?

angular bootstrap-modal viewchild
2个回答
8
投票

您可以打开模型的参考作为诺言并关闭它。

editDocumentRow = function (documentId, modal) {
  this.http
    .get(ConstantComponent.url + "/documentmanagerapi/Get/" + documentId)
    .map((res: Response) => res.json())
    .subscribe((data) => {
      this.documentsRowDetails = data;
      this.modalService.open(modal);
      this.modalReference = this.modalService.open(modal);
      this.modalReference.result.then(
        (result) => {
          this.closeResult = `Closed with: ${result}`;
        },
        (reason) => {
          this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
        }
      );
    });
};

现在您可以关闭模​​型

this.modalReference.close();

0
投票

打开模式:

constructor(
    private modalService: NgbModal
) { }

this.modalService.open()

关闭激活模式:

constructor(
    public activeModal: NgbActiveModal
) { }

editDocumentRow = function (documentId, modal) {
    this.activeModal.dismiss();
})
© www.soinside.com 2019 - 2024. All rights reserved.