Angular 子组件&x27;s Formcontrol值无法在提交时绑定
在我的父组件Angular 子组件&x27;s Formcontrol值无法在提交时绑定,angular,Angular,在我的父组件AddCustomerComponent中,我使用了AddressComponent,它是子组件。 下面是每个表单的表单控制代码 添加客户组件 createFormControl() { this.clientRegistrationForm = this.formBuilder.group({ orgName: new FormControl(this.customer.orgName, Validators.required), panNumber
AddCustomerComponent
中,我使用了AddressComponent
,它是子组件。
下面是每个表单的表单控制代码
添加客户组件
createFormControl() {
this.clientRegistrationForm = this.formBuilder.group({
orgName: new FormControl(this.customer.orgName, Validators.required),
panNumber: new FormControl(this.customer.panNumber, Validators.required),
domainName: new FormControl(
this.customer.domainName,
Validators.required
),
priContact: new FormControl(
this.customer.priContact,
Validators.required
),
priNumber: new FormControl(this.customer.priNumber, Validators.required),
priEmail: new FormControl(this.customer.priEmail, Validators.required),
secContact: new FormControl(this.customer.secContact),
secNumber: new FormControl(this.customer.secNumber),
secEmail: new FormControl(this.customer.secEmail),
address: this.addressForm.getAddressFromGroup(), // I am getting Address Form control
});
}
getAddressFromGroup() {
this.addressFormFields = this.formBuilder.group({
addressText: new FormControl(this.addr.addressText),
pincode: new FormControl(this.addr.pincode),
country: this.formBuilder.group({
id: new FormControl(this.country.id),
}),
state: this.formBuilder.group({
id: new FormControl(this.state.id),
}),
city: this.formBuilder.group({
id: new FormControl(this.city.id),
}),
});
return this.addressFormFields;
}
地址组件
createFormControl() {
this.clientRegistrationForm = this.formBuilder.group({
orgName: new FormControl(this.customer.orgName, Validators.required),
panNumber: new FormControl(this.customer.panNumber, Validators.required),
domainName: new FormControl(
this.customer.domainName,
Validators.required
),
priContact: new FormControl(
this.customer.priContact,
Validators.required
),
priNumber: new FormControl(this.customer.priNumber, Validators.required),
priEmail: new FormControl(this.customer.priEmail, Validators.required),
secContact: new FormControl(this.customer.secContact),
secNumber: new FormControl(this.customer.secNumber),
secEmail: new FormControl(this.customer.secEmail),
address: this.addressForm.getAddressFromGroup(), // I am getting Address Form control
});
}
getAddressFromGroup() {
this.addressFormFields = this.formBuilder.group({
addressText: new FormControl(this.addr.addressText),
pincode: new FormControl(this.addr.pincode),
country: this.formBuilder.group({
id: new FormControl(this.country.id),
}),
state: this.formBuilder.group({
id: new FormControl(this.state.id),
}),
city: this.formBuilder.group({
id: new FormControl(this.city.id),
}),
});
return this.addressFormFields;
}
我正在使用
@ViewChild(AddressComponent, { static: true }) addressForm: AddressComponent;
添加新记录时工作正常,添加记录时所有值都正确获取并保存到数据库中
问题陈述
onClickSubmit(data) {
console.log("saved data" + JSON.stringify(this.customer));
this.customer = data;
console.log('custoemr data', JSON.stringify(this.customer));
this.customerService.createCustomer(this.customer).subscribe(
(response) => {
if (response) {
this.router.navigateByUrl('/dashboard/customerlist');
sessionStorage.setItem('registered', 'true');
}
console.log(response);
},
(err) => {
console.log(err);
alert(err);
}
);
}
问题正在更新中,
我正在获取相应用户的预填充表单,但在提交数据时,我没有获取AddressComponent
的值,请查找下面的快照
屏幕截图中清楚地显示,所有值都填充在表单中,但在提交数据时,AddressComponent
的整个值为null
提交
onClickSubmit(data) {
console.log("saved data" + JSON.stringify(this.customer));
this.customer = data;
console.log('custoemr data', JSON.stringify(this.customer));
this.customerService.createCustomer(this.customer).subscribe(
(response) => {
if (response) {
this.router.navigateByUrl('/dashboard/customerlist');
sessionStorage.setItem('registered', 'true');
}
console.log(response);
},
(err) => {
console.log(err);
alert(err);
}
);
}
数据
只是clientRegistrationForm.value
我无法解决此问题,请提供任何帮助。在将数据填充到表单时,您可能会错过将数据添加到表单控件的机会,这会导致在执行提交时提供空表单对象 因此,请确保您正在将预填充的数据添加到相应的控件中 像
快乐编码…) 你能显示更新()的代码吗?是的,请查看我的编辑
console.log(“保存的数据”+JSON.stringify(this.customer))代码>当你点击提交时,这个在浏览器控制台中的值是什么?请检查附加的快照,你会在控制台中找到值。基本上,当你点击提交按钮输入所有表单值后,你将无法获得地址表单值?这意味着我需要在单个表单控件中添加值,是否有任何方法可以填充相应的模型,并且模型的所有值都成为自动填充是的,如果您想以formName.value的形式访问值,则需要单独添加,否则您可以在提交
方法中使用相应的模型。