Angular2 Update nested FormGroup value?

I have this inside the component:

private formBuilder: FormBuilder

...

signupForm: FormGroup;

...

this.signupForm = this.formBuilder.group({
  'name':             [null, Validators.required],
  'account':          this.formBuilder.group({
    'email':          [null, [Validators.required, ...]],
    'confirm_email':  [null, Validators.required],
  }, {validator: ValidationService.emailMatcher}),
  'password':         [null, [Validators.required,...]]
});

And I want to set the value for the email field. I tried this but no luck:

this.signupForm.patchValue({'email': 'myvalue@asd.com'});

But the value is nested, so what is the syntax in this case? I also tried:

this.signupForm.patchValue({'account.email': 'myvalue@asd.com'});

Also can be found here:

https://angular.io/docs/ts/latest/api/forms/index/FormGroup-class.html#!#patchValue-anchor

thank

+4
source share
1 answer

Try the following:

this.signupForm.patchValue({account:{email: 'myvalue@asd.com'}});

Another solution:

(<FormGroup>this.signupForm.controls['account']).controls['email'].patchValue('myvalue@asd.com');

Sorry for the bad indentation.

+12
source

Source: https://habr.com/ru/post/1667874/


All Articles