【发布时间】:2020-04-30 05:14:37
【问题描述】:
我在我的 HTML 中为 Angular 9 应用程序创建了一个表单组。在这个表单组中,有一个文件的上传功能。调用handleFileInput函数时上传正常,我可以通过它下面的控制台日志看到。但是,当我将表单组发送到我的服务时,文件属性仍然为 NULL。我知道这是因为它在我的构造函数中设置为 NULL,但是如何更改我的代码以便将表单组中的文件设置为上传的文件?根据我的阅读,表单组必须在构造函数中声明。
export class HelpComponent implements OnInit {
form: FormGroup;
srcPage = 'Test';
fileToUpload: File = null;
constructor(public fb: FormBuilder, private messageService: MessageService,
public exportFilesService: ExportFilesService) {
this.form = this.fb.group({
summary: new FormControl('', [Validators.required]),
description: new FormControl('', [Validators.required]),
isurgent: [false],
file: this.fileToUpload
});
}
ngOnInit() {
}
handleFileInput(files: FileList) {
this.fileToUpload = files.item(0);
console.log(this.fileToUpload);
}
submitForm() {
this.messageService.sendSupportRequest(this.form.get('summary').value ,
this.form.get('description').value, this.form.get('isurgent').value,
this.srcPage, this.form.get('file').value);
}
}
【问题讨论】:
标签: angular typescript formgroups