Angular2 set value for formGroup Angular2 set value for formGroup angular angular

Angular2 set value for formGroup


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)});

With this second technique, not all values need to be supplied and fields whos values were not set will not be affected.


You can use form.get to get the specific control object and use setValue

this.form.get(<formControlName>).setValue(<newValue>);


For set value when your control is FormGroup can use this example

this.clientForm.controls['location'].setValue({      latitude: position.coords.latitude,      longitude: position.coords.longitude    });