本文介绍了Angular2更新FormGroup的嵌套值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在组件内部拥有这个

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

但是该值是嵌套的,因此在这种情况下,sintax是什么?我也尝试过:

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

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

也在此处搜索:

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

谢谢

推荐答案

尝试一下:

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

另一个解决方案是:

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

很抱歉缩进不良.

这篇关于Angular2更新FormGroup的嵌套值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 10:45