我正在使用API,它会返回如下响应:
case:1
requests:[reqObj1,reqObj2,reqObj3]
count:3
message:"Found requests successfully."
pagesCount:1
服务:
listRequest(sort, records, pageNumber, queries?): Observable<any> {
const updateQueries = this.gtn.getHeaders() as RequestOptions;
updateQueries.params = queries;
return this.http
.get(globals.domain + '/team/requests/list/sortBy-' + sort + '/' + records + '/' + pageNumber + '/', updateQueries)
.map(res => {
this.responseData = res.json();
return this.responseData;
});
}
这将返回带有响应对象的Observable,如上所述,我将在组件中获得此可观察对象,并将其分配给组件中的本地observable。
组件:
this.requestResponse= this.rts.listRequest(sort, records, page, this.q);
我想迭代observable中的请求数组,我在我的html中使用异步管道
HTML:
<div class="request-items-inner" *ngFor="let request of requestResponse.requests | async">
{{request.createdAt}}
</div>
但这不会在屏幕上显示任何内容。如果我使用 * ngFor =“let request of requestResponse | async”,则会在控制台中出错。
答案 0 :(得分:4)
首先需要使用'async'管道解包响应,然后迭代请求数组。
<div class="request-items-inner" *ngFor="let request of (requestResponse | async)?.requests">
{{ request.createdAt }}
</div>