[英]Angular2 : Bind view with callback variable
在我的angular2項目中,我使用FileReader讀取了一個csv文件。 在onloadend
回調之后,我有一個變量,其中包含我的csv文件的內容。
這是我的component.ts:
items: Array<any> = []
...
readCSV (event) {
let csvFileParseLog = this.csvFileParseLog;
r.onloadend = function(loadedEvt) {
devicesFile = files[0];
let csvFileParseLog = [];
parseDevicesCsvFile(contents) // One of my function which is an observable
.subscribe(newItems=> {
csvFileParseLog.push(newItems); // My result
},
exception => { ... }
);
};
}
我試圖通過將我的值傳遞給items
來將csvFileParseLog
綁定到我的視圖中,但是沒有成功。
這是我的componenet.html:
<div *ngFor="let c of csvFileParseLog">
{{ c.value }}
</div>
如何將這些內容顯示到我的視圖組件中並使用ngFor對其進行循環?
r.onloadend = function(loadedEvt) {
應該
r.onloadend = (loadedEvt) => {
否則, this
將無法在該功能中使用。
然后就用
this.csvFileParseLog.push(newItems);
然后刪除let csvFileParseLog = this.csvFileParseLog;
您可能還需要注入
constructor(private cdRef:ChangeDetectorRef) {}
並在subscribe()
調用它
.subscribe(newItems=> {
this.csvFileParseLog.push(newItems);
this.cdRef.detectChanges();
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.