角度发布方法动态付款网关错误:TypeError:this.element.submit不是函数

问题描述

我在角的html端创建了post方法

<form  #bankUrlForm name="bankUrlForm"   action="{{bankTranactionModel.sendToBankUrl}}" method="post" target="_self"  >
  <input type="hidden" name="RefId" [ngModel]="bankTranactionModel.refId">
</form>

并从服务器获取bankTranactionModel数据 并正确填充数据,操作网址,refid,... 以及后面的代码

 @ViewChild('bankUrlForm') bankPostMethod;

并致电提交

this.bankPostMethod.submit();

我收到错误消息

TypeError: this.bankPostMethod.submit is not a function

enter image description here

enter image description here

而且我也使用过ngform,但出现错误

以及何时使用此

@ViewChild('bankUrlForm') bankPostMethod: ElementRef<HTMLFormElement>;

重定向页面显示错误

masspurchase:1 POST http://localhost:4200/purchase/masspurchase 404 (Not Found)

enter image description here

解决方法

@ViewChild('bankUrlForm')返回ElementRef中的HTMLFormElement,在您的情况下:

@ViewChild('bankUrlForm') bankPostMethod: ElementRef<HTMLFormElement>;

因此,要访问本机HTMLFormElement.submit()方法,您需要编写:

this.bankPostMethod.nativeElement.submit();
,

我必须这样做,而且行得通,也许在打字稿上使用javascript是错误的

var form = document.createElement("form");
          form.setAttribute("method",sendMethod);
          form.setAttribute("action",bankUrl);
          form.setAttribute("target","_self");
          var hiddenField = document.createElement("input");
          hiddenField.setAttribute("name","RefId");
          hiddenField.setAttribute("value",refIdValue);
          form.appendChild(hiddenField);
          document.body.appendChild(form);
          form.submit();
          document.body.removeChild(form);