【问题标题】:Angular 6 - Revert form fields changed values on submitAngular 6 - 在提交时恢复表单字段更改的值
【发布时间】:2021-05-30 08:10:56
【问题描述】:

在我的 Angular 6 应用程序中,有一个配置文件更新部分,配置文件表单最初加载预填充的详细信息,即注册期间保存的详细信息。

表格如下所示

标题、名字、姓氏、电子邮件地址是输出字段,因此无法编辑。

如果触摸新电子邮件地址将成为必填项,如果输入新电子邮件,则确认电子邮件是必填项。

用户可以编辑新的电子邮件地址、手机号码和日间电话号码字段。

在提交时,调用后端服务来更新用户配置文件。如果此服务关闭,我必须将更改的表单字段恢复为最初加载的方式。

我正在使用以下代码重置电子邮件、确认电子邮件、手机号码和白天电话号码。

updateProfile(){
    this.http.post('https://testurl/user/updateProfile', param, options).subscribe(
        data => {
            console.log("Profile update successful:", data);
        },
        error => {
            console.log("Profile update error!", error);
            if(error.status === '500'){ // If service is down
                this.profileForm.controls['newemail'].reset();
                this.profileForm.controls['confirmemail'].reset();
                this.profileForm.controls['mobile'].reset();
                this.profileForm.controls['daytimephone'].reset();          
            }
        }
    );
}

但这会产生以下错误。无论如何,重置只会清空字段。

ERROR TypeError: Cannot read property 'reset' of undefined

我想查找字段是否被修改并将其值恢复为初始值(如果存在)。

为了更清楚,假设用户修改了以下字段

new email => testabc10@gmail.com
confirm email => testabc10@gmail.com
Daytime Phone => +44123456789

当这个表单提交时假设后端宕机了,我怎样才能取回白天电话号码的初始值?

请指导,谢谢

【问题讨论】:

  • 后端响应成功后清除修改字段。直到不清除该领域。如果我错了,请纠正。
  • 您能告诉我如何获取修改后的字段吗?另外,我的意图不是清除字段值,而是保留其旧值( onload 时的值)
  • 我已经更新了我的问题
  • 可以获取修改后的字段值如this.profileForm.get('newemail').value等。您应该保留该值,直到 api 响应成功。在我们得到成功响应后,我们可以清除字段使用this.profileForm.get('newemail').reset() 和儿子。
  • 我明白你的意思,正如你所说,每个表单控件的值都可以保存,但重置只会使该字段为空,就我而言,我想用它所具有的值设置表单控件在用户进行更改之前。甚至重置也会出现以下错误ERROR TypeError: Cannot read property 'reset' of undefined

标签: angular typescript forms angular-reactive-forms onsubmit


【解决方案1】:

我想您有一种方法可以设置控件的初始值。可能就在加载配置文件之后。如果 Web 请求失败,调用相同的方法似乎是个好主意。

profile: Profile;

setProfileFormValue() {
   this.profileForm.controls['newemail'].setValue('');
   this.profileForm.controls['confirmemail'].setValue('');
   this.profileForm.controls['mobile'].setValue(this.profile.mobile);
   this.profileForm.controls['daytimephone']
      .setValue(this.profile.daytimephone);
}
updateProfile() {
   this.http.post('https://testurl/user/updateProfile', param, options)
      .subscribe(data => {
         console.log("Profile update successful:", data);
      },
      error => {
         console.log("Profile update error!", error); // console.error instead of console.log?
         if(error.status === '500') { // If service had an internal server error (not necessary down)
            this.setProfileFormValue();
         }
      });
}

【讨论】:

  • 试过this.myForm.markAsPristine()它使当前表单原始,所以所有输入的字段仍然相同
  • 那么答案中描述的整个算法呢? (:它有效吗?
  • 你能帮我解决一下sn-p pls的代码吗?也许我没有正确理解你。我刚刚做了这个updateProfile(){ this.http.post('https://testurl/user/updateProfile', param, options).subscribe( data => { console.log("Profile update successful:", data); }, error => { console.log("Profile update error!", error); if(error.status === '500'){ // If service is down this.myForm.markAsPristine() } } ); }
  • 自从我发布答案后,问题似乎发生了变化(或者我变得更聪明了,我不认为这是可能的)。答案看起来不再相关。我更新了答案。现在解决问题了吗? (:
猜你喜欢
  • 2012-12-11
  • 2015-04-13
  • 2019-04-23
  • 1970-01-01
  • 2019-12-18
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
  • 2022-07-09
相关资源
最近更新 更多