【问题标题】:Angular - patchValue FormControlAngular - patchValue FormControl
【发布时间】:2018-05-22 20:06:19
【问题描述】:

我有我在方法 saveConvention 中调用的 formGroup 波纹管,我想获取组合同并在 createConvention 服务中编辑 formControl conventionId,我尝试使用下面提到的方法但是当我尝试显示时,conventionId 总是空

  this.conventionForm = this.fb.group({
        shortname: fb.control(null, Validators.required),
        fullname: fb.control(null, Validators.required),
        contract: this.fb.group({
            conventionId: fb.control(null),
            signatureProvides: fb.control(null, Validators.required)
        })
    });


public saveConvention(data: FormGroup): void {
    let currentData: FormGroup = data;

    let formControlContract: AbstractControl = currentData.get('contract');

    data.removeControl('contract');
    this.conventionService.createConvention(data.value)
        .catch(err => {})
        .subscribe(res => formControlContract.patchValue({ conventionId: res.id }) );

   console.log(formControlContract.value);

   this.contractBenefitPartnerService.createContract(formControlContract.value)
         .catch(err => {}).subscribe(res => {});            
  }

【问题讨论】:

    标签: angular angular-forms


    【解决方案1】:

    使用patchValue后需要使用updateValueAndValidity才能生效。

    this.conventionService.createConvention(data.value)
        .catch(err => {})
        .subscribe(res => {
           formControlContract.patchValue({ conventionId: res.id }) 
           formControlContract.updateValueAndValidity();
        }
    );
    

    【讨论】:

      【解决方案2】:

      试试:

      formControlContract.patchValue({ contract: {conventionId: res.id }})

      【讨论】:

        猜你喜欢
        • 2021-09-02
        • 2021-06-08
        • 2020-02-11
        • 2019-12-19
        • 2016-11-05
        • 2022-11-11
        • 2018-07-13
        • 2019-01-25
        • 1970-01-01
        相关资源
        最近更新 更多