如何使用Angular2模型驱动表单设置子表单组的值?
问题描述:
单击按钮后,我正在更改选择框的值。我用模型驱动方法编写了整个表单。选择框位于子窗体组中。如何使用Angular2模型驱动表单设置子表单组的值?
this.profileForm = this.fb.group({
name: this.fb.control('',[ Validators.required, Validators.minLength(3) ]),
website: this.fb.control('',[Validators.pattern('^(http|https|ftp)?(://)?(www|ftp)?.?[a-z0-9-]+(.|:)([a-z0-9-]+)+([/?].*)?$')]),
contributor: this.fb.control('',[Validators.email]),
dealershipLevel: this.fb.control('single',[ Validators.required ]),
manufacturer: this.fb.control('',[ Validators.required ]),
address : this.fb.group({
addressline: this.fb.control('',[ Validators.required ]),
city: this.fb.control('',[ Validators.required ]),
state: this.fb.control('',[ Validators.required ]),
zip: this.fb.control('',[ Validators.required ])
})
})
我想设置一旦按钮被点击的状态选择框中的值。有可能使用的代码
this.profileForm.controls.manufacturer.setValue('Tvm', { onlySelf: true });
如何从子窗体集团控制设定的值来设置父窗体组的值?
答
this.profileForm.get('address.state').setValue('Tvm', { onlySelf: true });
答
试试这个:
this.profileForm.patchValue({address:{state:'Tvm'}}, { onlySelf: true });