在打字稿中循环遍历表单数据
Looping through formdata in typescript
我正在尝试遍历 formdata,我已经多次阅读我应该能够在 for ... of ... 循环中使用 FormData,但是当我尝试时,我得到了错误:
Type 'FormData' is not an array type or a string type
我的代码:
export class NewsCreationComponent implements OnInit {
fileToUpload: File = null;
uploadImages = new FormData();
...
handleFile(event) {
this.fileToUpload = event.target.files;
this.uploadImages.append(this.fileToUpload.name, this.fileToUpload);
}
save() {
for (let up of this.uploadImages) {
this.imageService.createImage(up)
});
}
我做错了什么?
将您的 FormData 初始化 uploadImages = new FormData();
移动到 ngOnInit()
生命周期挂钩,这样您就可以确定它是在您的 save()
函数被调用时定义的。
我正在尝试遍历 formdata,我已经多次阅读我应该能够在 for ... of ... 循环中使用 FormData,但是当我尝试时,我得到了错误:
Type 'FormData' is not an array type or a string type
我的代码:
export class NewsCreationComponent implements OnInit {
fileToUpload: File = null;
uploadImages = new FormData();
...
handleFile(event) {
this.fileToUpload = event.target.files;
this.uploadImages.append(this.fileToUpload.name, this.fileToUpload);
}
save() {
for (let up of this.uploadImages) {
this.imageService.createImage(up)
});
}
我做错了什么?
将您的 FormData 初始化 uploadImages = new FormData();
移动到 ngOnInit()
生命周期挂钩,这样您就可以确定它是在您的 save()
函数被调用时定义的。