问题描述
我在角的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
而且我也使用过ngform
,但出现错误。
以及何时使用此
@ViewChild('bankUrlForm') bankPostMethod: ElementRef<HTMLFormElement>;
masspurchase:1 POST http://localhost:4200/purchase/masspurchase 404 (Not Found)
解决方法
@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);