Angular2更新FormGroup的嵌套值? [英] Angular2 Update FormGroup nested value?

查看:150
本文介绍了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': 'myvalue@asd.com'});

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

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

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

也在此处搜索:

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

谢谢

推荐答案

尝试一下:

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

另一个解决方案是:

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

很抱歉缩进不良.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆