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 请求中应用一些错误处理?
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} );
}