[英]Angular 4 - argument of type string[] is not assignable to type parameter of type 'string'
[英]Argument of type 'string' is not assignable to parameter of type 'AbstractControl'. Angular Reactive Forms
我正在尝试使用材料形式将字符串值推入formArray中,但是它返回此错误:
Argument of type 'string' is not assignable to parameter of type 'AbstractControl'.
如果我尝试将完整的对象推入数组,则可以正常工作,但作为字符串值,则不能。 这是我声明formArray的地方:
this.maintenanceFormGroup = this._formBuilder.group({
title: '',
description: ['', Validators.required],
maintenance_images_url: this._formBuilder.array([]),
});
这是我尝试将字符串值推入数组的地方:
const pushDownloadUrlIntoMaintenancePhotosArray = flatMap(() => {
return this._storage.downloadURL
.map(url => {
console.log(url)
const controls = <FormArray>this.formGroup.controls.maintenance_images_url;
controls.push(url);
});
});
关于我为什么收到此错误的任何建议?
您应该初始化您的maintenance_images_url格式数组,例如:
const controls = <FormArray>this.maintenanceFormGroup.controls['maintenance_images_url'];
const urlControl = this.initUrl(url);
controls.push(urlControl);
initUrl(url) {
return this.formBuilder.group({
value: [url],
});
}
更新:
const controls = this.maintenanceFormGroup.get('maintenance_images_url');
if (!controls.value.includes(url)) {
controls.push(this.formBuilder.control(url));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.