我正在創建一個angular2項目,並且我正在使用ng2-uploader作爲文件上傳的插件。我想抓住一個div,並在同一時間我想要在div內的上傳按鈕。選擇一個文件上傳後,我得到的錯誤爲 TypeError:無法將undefined或null轉換爲對象。如何解決:TypeError:無法將undefined或null轉換爲對象
HTML代碼:
<div [hidden]="!onUpload" ngFileDrop [options]="options" (onUpload)="handleUpload($event)" [ngClass]="{'file-over': hasBaseDropZoneOver}" (onFileOver)="fileOverBase($event)">
<input type="file" ngFileSelect [options]="options" (onUpload)="handleUpload($event)">
<p><span>Response: {{ uploadFile | json }}</span></p>
</div>
組件是:
import { Component, OnInit, NgModule, NgZone } from '@angular/core';
@Component({
selector: 'app-fileselect',
templateUrl: './fileselect.component.html',
styleUrls: ['./fileselect.component.css']
})
export class FileSelectComponent implements OnInit {
zone: NgZone;
hasBaseDropZoneOver: boolean = false;
basicProgress: number = 0;
uploadFile: any;
constructor() {
this.zone = new NgZone({ enableLongStackTrace: false });//file upload
}
options: Object = {
url: 'http://localhost:4200/assets/documents'
};
handleUpload(data): void {
if (data && data.response) {
data = JSON.parse(data.response);
this.uploadFile = data;
this.zone.run(() => {
this.basicProgress = data.progress.percent;
});
}
}
fileOverBase(e: any): void {
this.hasBaseDropZoneOver = e;
}
}
我也面對這個問題,讓我知道html的完整結構, 主要是這個上傳按鈕的外部div。 –
當然我會更新我的問題 – sainu