【发布时间】:2019-06-13 09:55:17
【问题描述】:
我不断在数组旁边看到一个“i”图标
除非我单击分页,否则 ngFor 不会在首次加载时显示项目。那就是它显示整个列表的时间。
是不是因为那个 value below was evaluated just now ,无论如何要在角度 7 中解决这个问题?
我的代码对这个组件所做的是,
1)在主要组件模板我有一个属性绑定
例如[thisIstheDataForThisComponent] = thisIsTheData
2) 然后在此代码的组件中,我循环遍历 thisIstheDataForThisComponent 的内容,因为它在组件中被标记为 @Input 装饰器。
我已经在应用程序的其他部分进行了相同样式的属性绑定,但只有这个数组在第一次加载时没有显示
这是我提取数据的服务代码
public getData(url, headers): Observable<any>{
return this.http.get(config.SERVICE_URL + url, headers)
.pipe(
delay(300),
map((data: any) => (data)),
catchError(this.handleError)
)
}
这是我在 父 组件中的代码
public thisIstheDataForThisComponent= { count: 0, data: [] };
let myHttpOptions= {
headers: new HttpHeaders({
'HTTP_TOKEN': 'ASDF2SOMERANDOMTOKENHERE'
})
};
this.myCustomService.getData('/apiController/getData', myHttpOptions).subscribe((data) => {
if (data != '') {
for (var i = 0; i < data.data.length; i++) {
this.thisIstheDataForThisComponent.data.push({
country: data.data[i].country,
total: data.data[i].total,
numstuff: data.data[i].numstuff,
blahstuff: data.data[i].blahstuff,
thisstuff: data.data[i].thisstuff,
thatstuff: data.data[i].thatstuff
});
}
this.thisIstheDataForThisComponent.count = data.data.length;
}
});
this.thisIstheDataForThisComponent.data = this.thisIstheDataForThisComponent.data;
this.thisIstheDataForThisComponent.count = this.thisIstheDataForThisComponent.count;
那么这里是父组件模板
<article class="col-sm12">
<my-widget [thisIstheDataForThisComponent]='thisIstheDataForThisComponent'></my-widget>
</article>
这里是 child 组件
@Input()thisIstheDataForThisComponent: any;
然后在child组件模板中
<tr *ngFor="let items of thisIstheDataForThisComponent.data"><!--START TR-->
【问题讨论】:
-
您能否分享一下代码,显示您如何在父组件中填充 thisIsTheData 属性?它是由 Observable.subscribe() 填充的吗?
-
我已经修改了我的帖子并包含了一些代码 sn-ps。你能帮忙看看哪里做错了吗?
-
是的,它由可观察订阅填充