24

I have this inside a 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': '[email protected]'});

But the value is nested, so whats the sintax in this case? I also tried:

this.signupForm.patchValue({'account.email': '[email protected]'});

Also searched here:

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

Thanks

1 Answer 1

57

Try this:

this.signupForm.patchValue({account:{email: '[email protected]'}});

Another solution is:

(<FormGroup>this.signupForm.controls['account']).controls['email'].patchValue('[email protected]');

Sorry for bad indentation.

Sign up to request clarification or add additional context in comments.

3 Comments

That's it! Thanks a lot! I do not know why I didn't realize with the solution ... now it seems obvious!
How to use variable in place of account in your above solution.
you could use this syntax this.signupForm.patchValue({[variable]:{email: '[email protected]'}});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.