Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 提交函数中的角度错误处理只是挂起_Angular - Fatal编程技术网

Angular 提交函数中的角度错误处理只是挂起

Angular 提交函数中的角度错误处理只是挂起,angular,Angular,我希望我的错误处理不只是在提交表单时挂起,而是在div中以Angular的形式显示错误,到目前为止,我的提交函数如下所示: onSubmit() { if(this.serviceForm.invalid) { this.serviceForm.setErrors({ ...this.serviceForm.errors, 'required': true }); return; } //this.loading = true; thi

我希望我的错误处理不只是在提交表单时挂起,而是在div中以Angular的形式显示错误,到目前为止,我的提交函数如下所示:

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显示它们。告诉我它是否适用于您,这样我将为您做一些实现:)