【发布时间】:2018-04-17 00:47:52
【问题描述】:
我正在尝试填充嵌套的ReactiveForm,它被分成几个子表单组件。一旦 API 请求完成,我就可以填充父表单,但不能将子 FormArray 完全循环到子数据的计数。以下是我的代码中的 sn-ps:
编辑视图:edit.component.ts
@Component({
selector: 'app-edit',
templateUrl: './edit.component.html',
styleUrls: ['./edit.component.scss']
})
export class EditComponent implements OnInit {
public data: Service = new Service({
id: '',
title: '',
description: '',
service_extras: []
});
public routeParams: any = {};
constructor (
private route: ActivatedRoute,
private service: ServicesService
) { }
ngOnInit() {
this.setRouteParams();
}
setRouteParams() {
this.route.params.subscribe(params => {
this.routeParams = params;
// getting services using api call
this.getService(this.routeParams);
});
}
getService(params) {
this.service
.getService(params.id)
.subscribe((service: Service) => {
this.data = service;
});
}
}
我在基础edit.component.ts 组件中请求数据并将接收到的数据传递给父表单组件,它是EditComponent 的子组件
edit.component.html
<service-form [serviceFormData]="data"></service-form>
service-form.component.ts
@Component({
selector: 'service-form',
templateUrl: './service-form.component.html',
styleUrls: ['./service-form.component.scss']
})
export class ServiceFormComponent implements OnInit {
private _serviceFormData = new BehaviorSubject<Service>(null);
@Input()
set serviceFormData(value) {
this._serviceFormData.next(value);
}
get serviceFormData() {
return this._serviceFormData.getValue();
}
public service: Service;
public serviceForm: FormGroup;
constructor(
private fb: FormBuilder
) { }
ngOnInit() {
this.serviceForm = this.toFormGroup();
this._serviceFormData.subscribe(data => {
this.serviceForm.patchValue(data);
});
}
private toFormGroup(): FormGroup {
const formGroup = this.fb.group({
id: [ '' ],
title: [ '' ],
description: [ '' ]
});
return formGroup;
}
}
我在@Input var 的帮助下通过订阅其更改然后将值修补到表单来接收数据,现在一切正常,因为一旦收到数据,所有字段都会被填充。问题出在下面:
service-extra.component.ts
@Component({
selector: 'service-extra-form',
templateUrl: './service-extra.component.html',
styleUrls: ['./service-extra.component.scss']
})
export class ServiceExtraComponent implements OnInit {
private _extraFormData = new BehaviorSubject<ServiceExtra[]>([]);
@Input() serviceForm: FormGroup;
@Input()
set extraFormData(value: ServiceExtra[]) {
this._extraFormData.next(value);
}
get extraFormData() {
return this._extraFormData.getValue();
}
public extrasForm: FormGroup;
constructor(private fb: FormBuilder) { }
ngOnInit()
{
this.serviceForm.addControl('service_extras', new FormArray([]));
this._extraFormData.subscribe(data => {
this.addNew();
this.extras.patchValue(data);
});
}
private toFormGroup()
{
const formGroup = this.fb.group({
id: [''],
service_id: [''],
title: ['']
});
return formGroup;
}
public addNew()
{
this.extrasForm = this.toFormGroup(); (<FormArray>this.serviceForm.controls.service_extras).push(this.extrasForm);
}
public removeThis(i: number)
{
(<FormArray>this.serviceForm.controls.service_extras).removeAt(i);
}
get extras(): FormArray {
return this.serviceForm.get('service_extras') as FormArray;
}
}
ngOnInit 上面的代码除了添加单个 extras 表单(即使有两条记录)并填充该表单之外什么都不做,即使我删除了订阅部分并仅使用 this.addNew(),这种情况也是一样的。我怎么知道ServiceExtras 有多少条记录,以便我可以将那么多FormGroups 添加到FormArray。
编辑 1
Stackblitz Demo
编辑 2
问题是如果有两条来自 API 的记录用于额外服务,那么我无法生成两个 FormGroups 并用数据填充它们,因为在渲染时我无法检测到有多少记录来自服务额外来自 API。
【问题讨论】:
-
你能在 stackblitz 上演示你的问题吗?
-
@yurzui 我已经编辑了我的问题以添加 Stackblitz 演示
-
查看代码,还是没看懂是什么问题。您何时知道 ServiceExtras 有多少条记录?
-
@trungk18 实际上的问题是,如果他们有 2 条记录需要额外服务,那么它只会填充一个,而发生这种情况是因为我将 1 个表单组推送到
service_extra<FormArray>...我的问题是我无法检测到有多少 service_extra 记录来自 API,因此我可以生成那么多FormGroups并且推送到<FromArray>service_extras,这有意义吗?
标签: angular typescript mean-stack