Manually Set Value for FormBuilder Control

前端 未结 11 1064
执笔经年
执笔经年 2020-11-27 11:25

This is driving me nuts, I\'m under the gun and can\'t afford to spend another whole day on this.

I am trying to manually set a control value (\'dept\') within the c

相关标签:
11条回答
  • 2020-11-27 11:30

    You can use the following methods to update the value of a reactive form control. Any of the following method will suit for your need.

    Methods using setValue()

    this.form.get("dept").setValue(selected.id);
    this.form.controls["dept"].setValue(selected.id);
    

    Methods using patchValue()

    this.form.get("dept").patchValue(selected.id);
    this.form.controls['dept'].patchValue(selected.id);
    this.form.patchValue({"dept": selected.id});
    

    Last method will loop thorough all the controls in the form so it is not preferred when updating single control

    You can use any of the method inside the event handler

    deptSelected(selected: { id: string; text: string }) {
         // any of the above method can be added here
    }
    

    You can update multiple controls in the form group if required using the

    this.form.patchValue({"dept": selected.id, "description":"description value"});
    
    0 讨论(0)
  • 2020-11-27 11:30

    None of these worked for me. I had to do:

      this.myForm.get('myVal').setValue(val);
    
    0 讨论(0)
  • 2020-11-27 11:33

    @Filoche's Angular 2 updated solution. Using FormControl

    (<Control>this.form.controls['dept']).updateValue(selected.id)

    import { FormControl } from '@angular/forms';
    
    (<FormControl>this.form.controls['dept']).setValue(selected.id));
    

    Alternatively you can use @AngularUniversity's solution which uses patchValue

    0 讨论(0)
  • 2020-11-27 11:39

    In Angular 2 Final (RC5+) there are new APIs for updating form values. The patchValue() API method supports partial form updates, where we only need to specify some of the fields:

    this.form.patchValue({id: selected.id})
    

    There is also the setValue() API method that needs an object with all the form fields. If there is a field missing, we will get an error.

    0 讨论(0)
  • 2020-11-27 11:40

    Aangular 2 final has updated APIs. They have added many methods for this.

    To update the form control from controller do this:

    this.form.controls['dept'].setValue(selected.id);
    
    this.form.controls['dept'].patchValue(selected.id);
    

    No need to reset the errors

    References

    https://angular.io/docs/ts/latest/api/forms/index/FormControl-class.html

    https://toddmotto.com/angular-2-form-controls-patch-value-set-value

    0 讨论(0)
  • 2020-11-27 11:40

    Tip: If you're using setValue but not providing every property on the form you'll get an error:

    Must supply a value for form control with name: 'stateOrProvince'.

    So you may be tempted to use patchValue, but this can be dangerous if you're trying to update a whole form. I have an address that may not have stateOrProvince or stateCd depending upon whether it is US or worldwide.

    Instead you can update like this - which will use the nulls as defaults:

    this.form.setValue( { stateOrProvince: null, stateCd: null, ...address } );
    
    0 讨论(0)
提交回复
热议问题