Angular - patchValue FormControl

时间:2018-05-22 11:53:25

标签: angular angular-forms

我有方法saveConvention中调用的formGroup bellow,我想获得组合同并在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 => {});            
  }

2 个答案:

答案 0 :(得分:1)

尝试:

  

formControlContract.patchValue({合同: {conventionId:res.id}})

答案 1 :(得分:0)

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

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