【发布时间】:2020-04-06 06:32:36
【问题描述】:
我的 formData 使用响应式表单的输入:categoryForm。我必须这样做才能上传我的数据。但是当我提交我的 formData 和 console.log 时,我得到的只是:FormData {} 所以是一个空表单。但我不知道为什么,因为我用this.categoryForm.get('name').value 获取每个数据的值
我的响应式表单实际上是在接收数据,并且表单有所有数据,所以问题是 formData 没有获取数据。
html:
<form [formGroup]="categoryForm">
<ion-item mode="ios" lines="none" class="checkbox-tag rounded-tag">
<ion-icon name="eye"></ion-icon>
<ion-checkbox formControlName="lock"></ion-checkbox>
<div>
<ion-item>
<ion-input [disabled]="tagList?.length > 0" mode="md" formControlName="category" clearInput="true" placeholder="Tag" name="tagValue"></ion-input>
<ion-button (click)="addTag()" [disabled]="!categoryForm.valid || tagList?.length > 0" item-right icon-only>
<ion-icon name="checkmark"></ion-icon>
</ion-button>
</ion-item>
</div>
</form>
</ion-content>
<ion-footer">
<ion-button
[disabled]="!tagList?.length > 0"
(click)="apiSubmit()"
expand="block"
color="secondary"
fill="solid"
>POST</ion-button>
</ion-footer>
ts:
ngOnInit() {
this.storage.get('image_data').then((imageFile) => {
console.log(imageFile)
this.categoryForm.patchValue({
'image': this.storage.get('image_data')
});
this.storage.get('when').then((whenData) => {
this.categoryForm.patchValue({
'when': this.storage.get('when')
});
});
});
this.categoryForm = new FormGroup({
'lock': new FormControl(true),
'category': new FormControl('', Validators.compose([
Validators.maxLength(25),
Validators.minLength(1),
Validators.required
])),
'image': new FormControl(null),
'when': new FormControl(null),
});
}
apiSubmit() {
const formData = new FormData();
formData.append('lock', this.categoryForm.get('lock').value);
formData.append('category', this.categoryForm.get('category').value);
formData.append('image', this.categoryForm.get('image').value);
formData.append('when', this.categoryForm.get('when').value);
console.log(formData);
this.http.post<any>(`{this.url}`, formData, httpOptions).subscribe(
(res) => console.log(res),
(err) => console.log(err)
);
}
【问题讨论】:
-
你什么时候开始在你的帖子中包含足够的信息,以便这里有人可以帮助你?在过去的几天里,您已经以各种形式发布了同样的代码,但您从来没有给过Minimal, Reproducible Example。为什么有人对此投了赞成票,这超出了我的理解。
-
@R.理查兹 好的,对不起,你是对的,我在同样的问题上挣扎了几天,我尝试了很多东西,但没有任何效果。对不起,当我的问题很糟糕时,我认为很多代码会起到威慑作用。我添加了我的模板和其他相关的打字稿。
标签: javascript angular typescript forms ionic-framework