我想提交一份包含文件的表格。所以我使用ng2-file-upload
npm上传文件
component.html
是-
<input type="file" ng2FileSelect [uploader]="uploader" (onFileSelected)="onFileSelected($event)">
我的component.ts是-
public uploader: FileUploader = new FileUploader({});
uploadForm: FormGroup;
constructor(
private formBuilder: FormBuilder,
private userService: UserServiceService
) {
}
ngOnInit() {
this.uploadForm = this.formBuilder.group({
branch : ['cs', Validators.required],
document : [null, Validators.required],
semester : ['3', Validators.required],
});
}
//////////// File Converted into base64/////////////////
onFileSelected(event: EventEmitter<File[]>) {
const file: File = event[0];
readBase64(file)
.then(function(data) {
console.log(data);
this.uploadForm.patchValue({ // Showing error -- Potentially invalid reference access to a class field via 'this.'
document: data
});
});
}
我在onFileSelected()
函数中生成文件的base64,然后我想将该base64添加到我的formGroup
中的document
密钥中。但是当我尝试使用this.uploadForm.patchValue()
时,它显示了一个错误
Potentially invalid reference access to a class field via 'this.'
我只想将该data
添加到我的document
密钥uploadForm
中
发布于 2018-08-11 05:34:57
任一
.then(function(data) {
this.whateva
}.bind(this)); //here is the trick
或
.then(data=> {//here is the trick
this.whateva
});
我没有读剩下的代码。
https://stackoverflow.com/questions/51794270
复制相似问题