Angular 提交函数中的角度错误处理只是挂起
我希望我的错误处理不只是在提交表单时挂起,而是在div中以Angular的形式显示错误,到目前为止,我的提交函数如下所示:Angular 提交函数中的角度错误处理只是挂起,angular,Angular,我希望我的错误处理不只是在提交表单时挂起,而是在div中以Angular的形式显示错误,到目前为止,我的提交函数如下所示: onSubmit() { if(this.serviceForm.invalid) { this.serviceForm.setErrors({ ...this.serviceForm.errors, 'required': true }); return; } //this.loading = true; thi
onSubmit() {
if(this.serviceForm.invalid) {
this.serviceForm.setErrors({ ...this.serviceForm.errors, 'required': true });
return;
}
//this.loading = true;
this.uploading = true;
this.service.postRequest(this.serviceForm.value).subscribe((response: any) => {
console.log(response);//On success response
this.router.navigate(['/confirmation'],{queryParams: {value: response.result[0].display_value}});
}, (errorResponse: any) => {
console.log(errorResponse); //On unsuccessful response
});
}
}
你可以有:
error:string = "";
...
onSubmit() {
...
(errorResponse: any) => {
this.error = "Your custom error message, could be conditional on your type of error";
}
}
在您的html中:
<div *ngIf="error">
{{ error }}
</div>
{{error}}
我创建了一个类导出类ValidationItem{property:string;error:string;},然后我填充了一个验证项集合,然后在我的组件中用一个ngfor显示它们。告诉我它是否适用于您,这样我将为您做一些实现:)