angular formcontrol set value code example

Example 1: update formgroup value angular

To set all FormGroup values use, setValue:

this.myFormGroup.setValue({
  formControlName1: myValue1, 
  formControlName2: myValue2
});
To set only some values, use patchValue:

this.myFormGroup.patchValue({
  formControlName1: myValue1, 
  // formControlName2: myValue2 (can be omitted)
});

Example 2: reactive form programmatically set value

this.formRef.patchValue({
    field: value1,
    field2: value2,
    /* ... */
  });

Example 3: set angular FormControl setValue

const objRequest = {
  operacion: 'getValuesById',
  data: { id: this.id }
};

const response = this.paymentService.myHttp( objRequest );

response.pipe(first()).subscribe(
  objResponse => {
    this.checkoutForm.get( 'name' ).setValue( objResponse.collection[0].nombre );
    this.checkoutForm.get( 'email' ).setValue( objResponse.collection[0].email );
    this.checkoutForm.get( 'numAcount' ).setValue( objResponse.collection[0].numAcount );
    this.checkoutForm.get( 'bank' ).setValue( objResponse.collection[0].bank_id );
  },
  error => {
    console.log(error);
  }
);