2
有一次,我成功地使用observables在我的Angular 2应用程序中对数组中的对象进行迭代。在我的客户服务档案我有这样的:在对象中定位数组 - 角度2中的可观察对象
getByCategory(category: string) {
const q = encodeURIComponent(category);
return this.http.get
(`https://api.someurl.com/${this.ver}/clients/category/${q}?apikey=${this.key}`)
.map((response: Response) => response.json())
.catch(this.stageErrorsHandler);
}
stageErrorsHandler(error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
我当时我订阅我的组件的ngOnInit生命周期挂钩,像这样:
ngOnInit() {
this.clientService.getByCategory('consulting')
.subscribe(resRecordsData => this.records = resRecordsData,
responseRecordsError => this.errorMsg = responseRecordsError);
}
在组件中,我也开始记录是空数组:
records = [];
然后在我看来,我遍历数组的记录是这样的:
<tr *ngFor="let record of records">
现在我们在api/server上的数据配置已经改变了。我曾经把目标定在数组,即是这样的:
[
{
"id": "someid",
}
]
...我需要改变这个到现在针对所谓的“数据”的阵列,位于内的对象。它看起来像这样:
{
"count": 1000,
"data": [
{
"id": "someid",
}
]
}
简单的问题,我很难过:我如何在这样的对象内定位一个数组?我需要在对象内部定位数组,因为这是* ngFor需要的,以便能够迭代。
尝试让records.data记录?或初始化记录:this.records = resRecordsData.data? – floor
好主意。让我尝试一下。 – Muirik
让我知道它是否工作 – floor