简体   繁体   English

Angular2:将视图与回调变量绑定

[英]Angular2 : Bind view with callback variable

In my angular2 project, I read a csv file with FileReader. 在我的angular2项目中,我使用FileReader读取了一个csv文件。 After the onloadend callback I have a variable which contain the content of my csv file. onloadend回调之后,我有一个变量,其中包含我的csv文件的内容。

Here my component.ts : 这是我的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 => { ... }
       );
  };
}

I tried to bind csvFileParseLog to my view by passing my value into items ... whithout success. 我试图通过将我的值传递给items来将csvFileParseLog绑定到我的视图中,但是没有成功。

Here my componenet.html : 这是我的componenet.html:

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

How can I display this content into my view component and loop on it with ngFor ? 如何将这些内容显示到我的视图组件中并使用ngFor对其进行循环?

r.onloadend = function(loadedEvt) {

should be 应该

r.onloadend = (loadedEvt) => {

otherwise this won't work within that function. 否则, this将无法在该功能中使用。

and then just use 然后就用

this.csvFileParseLog.push(newItems);

and just drop let csvFileParseLog = this.csvFileParseLog; 然后删除let csvFileParseLog = this.csvFileParseLog;

You also might need to inject 您可能还需要注入

constructor(private cdRef:ChangeDetectorRef) {}

and call it in subscribe() 并在subscribe()调用它

       .subscribe(newItems=> {
           this.csvFileParseLog.push(newItems); 
           this.cdRef.detectChanges();
       },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM