在Angular中,如果你想在响应式表单中使用文件字段,并且遇到了错误,以下是一种可能的解决方法:
fileControl: FormControl;
constructor() {
this.fileControl = new FormControl(null);
}
ngOnInit() {
this.formGroup = new FormGroup({
file: this.fileControl
});
}
onFileChange(event) {
const file = event.target.files[0];
this.fileControl.setValue(file);
}
这样,你就可以在响应式表单中使用文件字段了。当文件输入字段发生更改时,文件控制器的值将被更新,你可以通过订阅表单组的值变化来获取文件的值。
希望这个解决方法对你有所帮助!