Angular7 Formarray不显示从服务/api收到的所有记录
我试图将从webapi接收到的对象修补为角度反应形式。表单也有一个formarray。但是,尽管有超过3条或更多的记录,但只有2条记录被修补到反应表单的格式阵列中Angular7 Formarray不显示从服务/api收到的所有记录,angular7,angular-reactive-forms,formarray,webapi2,Angular7,Angular Reactive Forms,Formarray,Webapi2,我试图将从webapi接收到的对象修补为角度反应形式。表单也有一个formarray。但是,尽管有超过3条或更多的记录,但只有2条记录被修补到反应表单的格式阵列中 //initialization of form this.noseriesForm = this.fb.group({ id: [null], description: ['', Validators.required], code: [ '', Validators.compose([V
//initialization of form
this.noseriesForm = this.fb.group({
id: [null],
description: ['', Validators.required],
code: [ '', Validators.compose([Validators.maxLength(10), Validators.required])],
noSeriesList: this.fb.array([
this.initLine(), this.initLine()
])
});
//patching the object received from service to form
this.route.params.subscribe(routeParam => {
if (routeParam.id) {
this.noseriesService.get(routeParam.id)
.subscribe((data: NoSeries) => {
this.isCreateMode = false;
this.noseries = data;
this.noseriesForm.patchValue(this.noseries);
console.log(this.noseries, 'data from api');
console.log(this.noseriesForm.value,'formvalue');
});
}
});
//initialise formArray
initLine(): FormGroup {
return this.fb.group({
id: [null],
startingNoSeries: ['', Validators.required],
endingNoSeries: '',
lastUsedSeries: '',
effectiveDate: [null],
endingDate: [null],
noSeriesId: [null]
});
}
我有两个实体noseries和noseriesList,其中一个noseries有零个或多个noseriesList。因此,在从webapi获得noseries之后,我想将noseries的属性和导航列表“NoseriesList”修补为反应形式。
其余属性正在正确修补,但只有2条导航列表记录“NosriesList”正在修补到嵌套在反应表单中的formArray
//initialization of form
this.noseriesForm = this.fb.group({
id: [null],
description: ['', Validators.required],
code: [ '', Validators.compose([Validators.maxLength(10), Validators.required])],
noSeriesList: this.fb.array([
this.initLine(), this.initLine()
])
});
//patching the object received from service to form
this.route.params.subscribe(routeParam => {
if (routeParam.id) {
this.noseriesService.get(routeParam.id)
.subscribe((data: NoSeries) => {
this.isCreateMode = false;
this.noseries = data;
this.noseriesForm.patchValue(this.noseries);
console.log(this.noseries, 'data from api');
console.log(this.noseriesForm.value,'formvalue');
});
}
});
//initialise formArray
initLine(): FormGroup {
return this.fb.group({
id: [null],
startingNoSeries: ['', Validators.required],
endingNoSeries: '',
lastUsedSeries: '',
effectiveDate: [null],
endingDate: [null],
noSeriesId: [null]
});
}
记录从服务接收的数据显示3条noseriesList记录,而记录formvalue仅显示2条noseriesList记录。当您第一次初始化表单数组并添加两个空控件时。这就是为什么当您将值修补到formgroup时,只有这两个空控件被填充。在修补值之前,您应该在formarray中填入要修补的控件数
//将从服务接收的对象修补到表单
this.route.params.subscribe(routeParam=>{
if(routeParam.id){
this.noseriesService.get(routeParam.id).subscribe((数据:NoSeries)=>{
this.isCreateMode=false;
this.noseries=数据;
const nsList=this.noseriesForm.get(“noSeriesList”)作为FormArray;
nsList.clear();
this.noseries.forEach(=>nsList.push(this.initLine());
this.noseriesForm.patchValue(this.noseries);
log(this.noseries,“来自api的数据”);
log(this.noseriesForm.value,'formvalue');
});
}
});