处理表格POST提交

时间:2019-04-02 16:39:41

标签: angular forms

HTML:

<form [formGroup]="fgTest" action="http://..." target="_self"  method="POST" 
 #testCase>
  <input type="hidden" formControlName="test" name="test" />
</form>

打字稿:

@ViewChild("testCase") testCase;

ngOnInit() {
  this.fgTest = this.fb.group({
      "test": JSON.stringify(...)
    });
}

ngAfterViewInit() {
   this.testCase.nativeElement.submit();
}

我的问题是,如果重定向到另一个URL的action由于某种原因失败,如何通知Angular?是否可以在客户端的此POST请求中应用一些错误处理

1 个答案:

答案 0 :(得分:0)

Angular不能那样工作。以角度控制的形式,您应该处理“提交”或至少处理了一次按钮单击,然后依次单击“开始”和“ http”请求。

<form [formGroup]="fgTest" 
 #testCase>
  <input type="hidden" formControlName="test" name="test" />
<button (click)="submitForm()">
</form>

然后在代码文件中...

submitForm(){
   this.httpClient.post("url", data).subscribe((result)=> {do stuff} );
}