2017-01-26 6 views
4

私はコンポーネント内にこれを持っている: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,...]] 
}); 

そして私は、電子メールフィールドの値を設定したいです。私はこれを試しましたが、運はありません:

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

しかし値はネストされていますので、この場合のsintaxは何ですか?私も試してみました:

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

もここで検索:

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

を感謝

答えて

12

はこのお試しください:

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

別の解決策を次のとおりです。

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

申し訳ありません。

+0

これだけです!どうもありがとう!なぜ私は解決策に気付かなかったのかわかりません...今明らかです! – Ismaestro

+0

多くの時間を節約できました。 – roshini

+0

上記のソリューションでaccountの代わりにvariableを使用する方法。 –

関連する問題