Angular setValue, patchValue

Angular setValue
Sets the value of the Form Group. It accepts an object that matches the structure of the group, with control names as keys.

Set the complete value for the form group
const form = new FormGroup({
  first: new FormControl(),
  last: new FormControl()
});
console.log(form.value); // {first: null, last: null}
form.setValue({first: 'Nancy', last: 'Drew'});
console.log(form.value); // {first: 'Nancy', last: 'Drew'}

patchValue()
Patches the value of the FormGroup. It accepts an object with control names as keys, and does its best to match the values to the correct controls in the group.

this.createUserform.patchValue({
  firstname: 'Johan',
  lastname: 'doe',
  email: 'a@a.com',
});

2 Replies to “Angular setValue, patchValue”

Comments are closed.