angular - 在打字稿中循环表单数据
问题描述
我正在尝试遍历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()
调用函数时定义的。
推荐阅读
- java - java类变成了错误的东西
- node.js - ForEach 之后的代码在循环完成之前运行
- google-cloud-endpoints - 如何限制我的端点 api 发现
- asp.net-core-2.2 - 防止在 asp.net core 2.2 中重定向到 /Account/Login
- python - 龙卷风中的异步中间件
- ruby-on-rails - Opentok error in rails app, "OT is not defined"
- c - 我有两个 char 数组,当它们的大小不同时,程序无法正确读取/写入它们
- android - 如何为可在运行时轻松更改的 RecyclerView 网格的所有项目设置唯一高度?
- java - 如何从 MainActivity onCreate 发送事件到 React Native?
- android - 访问activity_main.xml文件时在android studio中“等待构建完成”