【问题标题】:How can I only send edited/changed fields in angular 2+ reactive form when I edit?编辑时,如何仅以角度 2+ 反应形式发送编辑/更改的字段?
【发布时间】:2018-10-31 23:01:58
【问题描述】:

我想在编辑时只发送表单中编辑/更改的值。

目前,我的表单发送所有表单值。

我正在使用接口:

export interface WorkExperience {
  id?: number;     
  name_company:string;     
  date_start:string;
  date_end:string;     
}
export interface WorkExperienceUpdate {
  id_work_experience: number;
  experience: WorkExperience;
}

我在我的 .ts 上执行此操作:

经验:工作经验;

this.experience = this.navParams.get('experience');

this.registrationForm = formBuilder.group({         
        company: [this.experience.name_company, Validators.required],         
        date_start: [this.formatDate(this.experience.date_start), Validators.required],
        date_end: [this.experience.date_end ? this.formatDate(this.experience.date_end) : '']            
      });

我的html是:

<form [formGroup]="registrationForm">              
          <ion-item>
            <ion-label floating>Company</ion-label>
            <ion-input type="text" formControlName="company"></ion-input>
          </ion-item>              
            <ion-label floating>Start Date</ion-label>
            <ion-datetime displayFormat="DD-MM-YYYY" formControlName="date_start"></ion-datetime>
          </ion-item>
          <ion-item>
            <ion-label floating>End Date</ion-label>
            <ion-datetime displayFormat="DD-MM-YYYY" formControlName="date_end"></ion-datetime>
          </ion-item>
        </form>

我使用相同的表单添加和编辑,所以我在编辑时只需要发送不一样的元素,像这样:

  1. 如果只编辑结束日期,对象应该是:

{ “id_education_experience”:8, “教育经验”:{ “日期结束”:“2017 年 30 月 10 日”
} }

现在以我的方式发送所有值。

我的提交是:

 submitRegistration(value): void {
    // EDIT
     let tempExp;
     tempExp = {          
              date_start: this.formatDate(value['date_start']),
              date_end: value['date_end'] ? this.formatDate(value['date_end']) : null,
              name_company: value['company'],         
            }

 let updateExperience: WorkExperienceUpdate = {
        experience: tempExp,
        id_work_experience: this.experience.id,
      };

 this._wp.updateWorkExperience(updateExperience).subscribe(r => {
        this.presentToast(`La experiencia laboral se ha modficado`);
        this.nav.pop().then(() => this.presentToast(`La experiencia laboral se ha modficado`));
      }, error => {
        this.nav.pop().then(() => {
          this.presentToast(`Ups, ha ocurrido un error.Inténtalo de nuevo`);
        });
      })
    }

我怎样才能做到只发送已编辑的值

提前谢谢你

【问题讨论】:

    标签: angular typescript ionic3


    【解决方案1】:

    您可以检查表单的 dirty 值。

    getDirtyValues(cg: FormGroup) {
      const dirtyValues = {};
      Object.keys(cg.controls).forEach(c => {
        const currentControl = cg.get(c);
    
        if (currentControl.dirty) {
          dirtyValues[c] = currentControl.value;
        }
      });
      return dirtyValues;
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-20
      • 2017-04-24
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 2018-12-28
      • 2018-08-03
      • 2022-01-20
      • 1970-01-01
      相关资源
      最近更新 更多