如何在Angular2中的for循环中调用每个项目的最佳方法
<div class="post" *ngFor="let post of posts">
Title: {{post.title}}
Comments: {{getCommentsForPostID(post._id)}}
</div>
我不认为这是这样做的,所以我需要另一种解决方案。
更新
getCommentsForPostID(post_id) {
this.commonService.getMethod('posts/' + post_id + '/comments').then((data) => {
return data.result
}).catch((error) => {
return null
})
}
加载帖子的代码:
this.commonService.getMethod('posts').then((data) => {
this.posts = data.result
}).catch((error) => {
})
我用于http请求的公共服务:
getMethod(path) {
return this.http.get(this.api + path, { headers: this._makeHeaders()
}).toPromise().then((response) => {
return response.json();
}).catch(this.handleError);
}