admin管理员组文章数量:1023745
I am trying to push a string value into a formArray using material forms, however it is returning this error:
Argument of type 'string' is not assignable to parameter of type 'AbstractControl'.
If I try and push a full object into the array it works fine, but as a string value it doesn't. This is where I declare the formArray:
this.maintenanceFormGroup = this._formBuilder.group({
title: '',
description: ['', Validators.required],
maintenance_images_url: this._formBuilder.array([]),
});
and this is where I try and push the string value(s) into the 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);
});
});
Any suggestion as to why I am getting this error?
I am trying to push a string value into a formArray using material forms, however it is returning this error:
Argument of type 'string' is not assignable to parameter of type 'AbstractControl'.
If I try and push a full object into the array it works fine, but as a string value it doesn't. This is where I declare the formArray:
this.maintenanceFormGroup = this._formBuilder.group({
title: '',
description: ['', Validators.required],
maintenance_images_url: this._formBuilder.array([]),
});
and this is where I try and push the string value(s) into the 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);
});
});
Any suggestion as to why I am getting this error?
Share Improve this question asked Jul 31, 2018 at 11:32 Jm3sJm3s 6172 gold badges13 silver badges26 bronze badges 5- Why pushing a string into FormArray which holds Controls of a form? Can you explain what is behin this._storage.downloadURL? – jmachnik Commented Jul 31, 2018 at 11:41
- @jmachnik a string value for the Url of an image is being stored there – Jm3s Commented Jul 31, 2018 at 11:45
- And the part with title: '' works well? Shouldn't it be title: [''] etc – jmachnik Commented Jul 31, 2018 at 11:48
-
1
controls.push(new FormControl(url))
– yurzui Commented Jul 31, 2018 at 12:07 -
Your variable name is likely confusing you.
controls
is not actually an array of controls, it is yourmaintenance_images_url
FormArray. Checking the docs showsFormArray.push()
is expecting an AbstractControl and you are pushing a string instead. – Joseph Webber Commented Jul 31, 2018 at 12:48
1 Answer
Reset to default 1you should initalize your maintenance_images_url formarray, for e.g:
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],
});
}
update:
const controls = this.maintenanceFormGroup.get('maintenance_images_url');
if (!controls.value.includes(url)) {
controls.push(this.formBuilder.control(url));
}
I am trying to push a string value into a formArray using material forms, however it is returning this error:
Argument of type 'string' is not assignable to parameter of type 'AbstractControl'.
If I try and push a full object into the array it works fine, but as a string value it doesn't. This is where I declare the formArray:
this.maintenanceFormGroup = this._formBuilder.group({
title: '',
description: ['', Validators.required],
maintenance_images_url: this._formBuilder.array([]),
});
and this is where I try and push the string value(s) into the 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);
});
});
Any suggestion as to why I am getting this error?
I am trying to push a string value into a formArray using material forms, however it is returning this error:
Argument of type 'string' is not assignable to parameter of type 'AbstractControl'.
If I try and push a full object into the array it works fine, but as a string value it doesn't. This is where I declare the formArray:
this.maintenanceFormGroup = this._formBuilder.group({
title: '',
description: ['', Validators.required],
maintenance_images_url: this._formBuilder.array([]),
});
and this is where I try and push the string value(s) into the 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);
});
});
Any suggestion as to why I am getting this error?
Share Improve this question asked Jul 31, 2018 at 11:32 Jm3sJm3s 6172 gold badges13 silver badges26 bronze badges 5- Why pushing a string into FormArray which holds Controls of a form? Can you explain what is behin this._storage.downloadURL? – jmachnik Commented Jul 31, 2018 at 11:41
- @jmachnik a string value for the Url of an image is being stored there – Jm3s Commented Jul 31, 2018 at 11:45
- And the part with title: '' works well? Shouldn't it be title: [''] etc – jmachnik Commented Jul 31, 2018 at 11:48
-
1
controls.push(new FormControl(url))
– yurzui Commented Jul 31, 2018 at 12:07 -
Your variable name is likely confusing you.
controls
is not actually an array of controls, it is yourmaintenance_images_url
FormArray. Checking the docs showsFormArray.push()
is expecting an AbstractControl and you are pushing a string instead. – Joseph Webber Commented Jul 31, 2018 at 12:48
1 Answer
Reset to default 1you should initalize your maintenance_images_url formarray, for e.g:
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],
});
}
update:
const controls = this.maintenanceFormGroup.get('maintenance_images_url');
if (!controls.value.includes(url)) {
controls.push(this.formBuilder.control(url));
}
本文标签:
版权声明:本文标题:javascript - Argument of type 'string' is not assignable to parameter of type 'AbstractControl'. 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745607014a2158783.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论