从Angular中的Submit事件获取Iframe

时间:2018-10-25 13:11:29

标签: angular forms iframe

我正在尝试从Angular中的iframe检索提交表单事件。我似乎无法实现。

ngAfterViewInit() {
    setTimeout(() => {
      const innerDoc = this.iframe.nativeElement.contentDocument || this.iframe.nativeElement.contentWindow.document;
      const form = innerDoc.querySelector('form'); // correctly returns the form
      form.addEventListener('submit', (e) => {
      // it will never reach this part
        e.preventDefault();
        this.toastr.success('Le processus a bien été lancé');
        window.parent.postMessage('close', '*');
      }, false);
    }, 2500);
  }

但是,它正在处理点击事件。

ngAfterViewInit() {
    setTimeout(() => {
      const innerDoc = this.iframe.nativeElement.contentDocument || this.iframe.nativeElement.contentWindow.document;
      const button = innerDoc.getElementsByClassName('submit-button'); // correctly returns the button
      button[0].addEventListener('click', () => {
      // it does reach this part when I click into the button
        this.toastr.success('Le processus a bien été lancé');
        window.parent.postMessage('close', '*');
      }, false);
    }, 2500);
  }

但是我不喜欢这种解决方案,因为它有很多流程。 知道如何正确实现提交addEventListener吗?谢谢!

0 个答案:

没有答案