2017-01-23 68 views
1

在我的angular2項目中,我使用FileReader讀取了一個csv文件。在onloadend回調之後,我有一個包含我的csv文件內容的變量。Angular2:使用回調變量綁定視圖

這裏我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 => { ... } 
     ); 
    }; 
} 

我試圖通過我的價值結合csvFileParseLog我的看法到items ......成功的內部消除。

這裏我componenet.html:

<div *ngFor="let c of csvFileParseLog"> 
    {{ c.value }} 
</div> 

我怎樣才能顯示此內容在我的視圖組件和環路上它與ngFor?

+0

如果你能分享你迄今爲止的代碼,這將是非常有幫助的。 :) – toskv

+0

當然是的:)我做到了! – onedkr

回答

2
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(); 
     },