angular - 提交表单超时后如何在页面上显示错误?
问题描述
我正在研究一种将数据提交到使用 Spring Boot 编写的后端休息服务的 Angular 表单。我需要添加一个功能,以在一定时间后数据提交失败的情况下显示错误。目前,具有表单的页面会一直加载,并且不会在表单上显示任何错误。我想为 Rxjs 使用超时功能,但我不知道如何实现它。
我的特定表单提交的代码片段如下:
this.apiService.afisEnroll(applicant).subscribe(data => {
if (data.response_code == 210) {
this.blockUI.stop();
console.log(`applicant %s enrolled successfully`, applicant.pbuNo);
var obj = JSON.stringify(this.applicants[this.applicants.length - 1]);
if (obj == JSON.stringify(applicant)) {
console.log("this is the last applicant");
this.captureEnrollmentDetailsForCOMPAS();
}
}
else {
this.blockUI.stop();
return this.toast.error("Applicant is already enrolled");
}
},
(err) => {
console.error(err);
this.blockUI.stop();
return this.toast.error("error enrolling applicant");
});
解决方案
使一个可变的布尔类型最初将其分配为 false,例如 var codeExecute =false; 做你所有的工作
setTimeout(function() {
this.apiService.afisEnroll(applicant).subscribe(data => {
if (data.response_code == 210) {
codeExecute =true; // addition code
this.blockUI.stop();
console.log(`applicant %s enrolled successfully`,applicant.pbuNo);
if
(JSON.stringify(this.applicants[this.applicants.length - 1]) ==
JSON.stringify(applicant)) {
console.log("this is the last applicant");
this.captureEnrollmentDetailsForCOMPAS();
}
//this.applicantenrolledsuccessfully = true;
}
else {
this.blockUI.stop();
return this.toast.error("Applicant is already enrolled");
}
}, (err) => {
console.error(err);
this.blockUI.stop();
return this.toast.error("error enrolling applicant");
});
}, 1000);
if(!codeExecute){
codeExecute = true;
return this.toast.error("Time out error Please try again");
}
推荐阅读
- bash - 如何从文件中读取并将其内容转换为bash中的多个变量
- amazon-web-services - 使用 AWS Corda Enterprise 模板在 Corda 节点之间执行事务时无法连接到负载均衡器
- codeigniter - 将 form_validation 调用设为私有
- javascript - 可以将 JavaScript 编译/编码为二进制以隐藏代码吗?
- bash - 比较 s3 存储桶和 csv 文件之间的行并发送到 s3
- java - Java Netbeans中的子字符串
- javascript - 在 HTML 上使用 AngularJS 输出数据的问题
- javascript - 错误“'EventTarget'类型上不存在属性'innerText'”?
- php - 选择 WooCommerce 交付方式后选择日期和时间
- oop - 默认情况下,私有属性是否被 .perl 和 .gist 隐藏