[英]Cannot read property 'value' of null angular reactive form get value
{
"estimate_number": "2020-1234",
"brand": "PF",
"floor": "Laminaat",
"floor_type": "Normaal",
"plint_type": "Hoog",
"floor_installer": {
"id": "14",
"name": "Maestro"
},
"address": {
"street": "Straatnaam 19",
"city": "Amsterdam",
"postal_code": "1111AB",
"province": "Noord-Holland"
},
"notes": "Test note"
}
why does const city = this.addEventForm.get('address.city').value;
为什么const city = this.addEventForm.get('address.city').value;
give me a value (in this case Amsterdam) but const floorInstaller = this.addEventForm.get('floor_installer.id').value;
给我一个值(在这种情况下是阿姆斯特丹)但const floorInstaller = this.addEventForm.get('floor_installer.id').value;
gives me the following error in developer console saying ERROR TypeError: Cannot read property 'value' of null at CalendarComponent.push..
在开发人员控制台中给我以下错误,说ERROR TypeError: Cannot read property 'value' of null at CalendarComponent.push..
This object is the result of a reactive angular form.该对象是反应角形式的结果。
The FormBuilder code used to make up the form:用于构成表单的 FormBuilder 代码:
// FormBuilder
const datesGroup = this.fb.group({
start_date: '',
start_time: { hour: 7, minute: 30 },
end_date: '',
end_time: { hour: 17, minute: 30 },
});
const addressGroup = this.fb.group({
street: '',
city: '',
postal_code: '',
province: '',
});
this.addEventForm = this.fb.group({
event_dates: datesGroup,
estimate_number: '',
brand: '',
floor: '',
floor_type: '',
plint_type: '',
floor_installer: { id: '', name: '', },
address: addressGroup,
notes: '',
});
Hi think you may get the value with below snippet:嗨,您可以通过以下代码段获得价值:
this.addEventForm.get("address").get("city").value
Code snippet I tried:我试过的代码片段:
const addressGroup = this.fb.group({
city: ""
});
this.addEventForm = this.fb.group({
address: this.fb.group({
city: ""
})
});
getVAlue() {
console.log(this.addEventForm.get("address").get("city").value);
}
HTML: HTML:
<form [formGroup]="addEventForm">
<div formGroupName="address">
<input formControlName="city" >
</div>
</form>
<button (click)="getVAlue()">Get</button>
Hope this will help.希望这会有所帮助。 I just took only city.我只拿了城市。 You may include all the controls.您可以包含所有控件。
May be the direct object { id: '', name: '', }
isn't recognized by the get()
function.可能是get()
函数无法识别直接对象{ id: '', name: '', }
。 Try assigning floor_installer
it's own form builder group.尝试将floor_installer
分配给它自己的表单构建器组。
const floorInstallerGroup = this.fb.group({
id: '',
name: ''
});
this.addEventForm = this.fb.group({
.
.
.
floor_installer: floorInstallerGroup,
.
.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.