如何使用Angular2模型驱动表单设置子表单组的值?

时间:2017-04-10 10:02:39

标签: angular

我希望在点击按钮后更改选择框的值。我已经在模型驱动方法中编写了整个表单。选择框位于子表单组内。

 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 ])

    })
})

单击按钮后,我想设置状态的值。可以使用代码

设置父Form组的值

this.profileForm.controls.manufacturer.setValue('Tvm', { onlySelf: true });

如何在子表单组中设置from控件的值?

2 个答案:

答案 0 :(得分:1)

this.profileForm.get('address.state').setValue('Tvm', { onlySelf: true });

答案 1 :(得分:0)

试试这个:

this.profileForm.patchValue({address:{state:'Tvm'}}, { onlySelf: true });