【发布时间】:2017-10-27 10:24:10
【问题描述】:
我有一个这样的模板
Component.html
<h1>aditya</h1>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<table class="table table-bordered">
<thead>
<tr>
<th style="white-space: nowrap" *ngFor="let columns of data?.display_columns; let i = index">{{columns}}</th>
</tr>
<tr *ngFor="let row1 of data?.row1; let i= index">
<td style="white-space: nowrap" *ngFor="let columns of data?.display_columns; let i = index">{{row1[columns]}}</td>
</tr>
</thead>
</table>
</div>
</div>
Component.ts
import { Component, OnInit } from '@angular/core';
import { HttpModule } from '@angular/http';
import { SpreadsheetService } from '../dataservice/spreadsheet.service';
@Component({
moduleId: module.id,
selector: 'sd-list-view',
templateUrl: 'list-view.component.html',
styleUrls: ['list-view.component.css'],
providers: [HttpModule, ListService]
})
export class ListViewComponent implements OnInit {
data: any;
spreadsheet: any;
data1: any;
isDataAvailable: boolean = false;
constructor(public _spreadsheetService: SpreadsheetService) {
}
ngOnInit() {
this._spreadsheetService.getData()
.subscribe(data => {
this.data = data;
(<any>window)['mydata'] = (data);
console.log(this.data.display_columns);
var data1: any;
//alert('get data');
return this.data;
});
//alert('list view 2');
}
}
模板“Aditya”的第一行正在显示,但循环内的数据表未加载到组件初始化中。
如何在视图初始化之前运行循环并加载数据,或者在视图初始化之后加载表数据?
PS:循环已经过测试并且可以正常工作。我不想使用 APP_Initializer。
I have gone through this question 但无法理解解决方案,因为我使用的是 Observables 而不是 Promise。
提前致谢。
【问题讨论】:
-
链接问题的哪一部分你不明白?
-
@GünterZöchbauer 我无法理解您在等待承诺首先加载的答案中的这一行 - “ngOnInit() { this.fetchEvent().then(() => console.log( this.ev)); // Now has value; }" 如果我必须做同样的事情,即在模板渲染之前等待 Observable 加载并返回数据,在我的情况下怎么做?
-
只需使用
subscribe()而不是then() -
您传入的数据实际上是什么样的?
标签: angular