【发布时间】:2018-08-04 02:13:30
【问题描述】:
我正在使用HttpClient 编写一个显示电影放映时间的Angular 4 应用程序。数据所在的 JSON 文件有 2 个:showtimes.json 和 movies.json。
// showtimes.json
[{
"id": "2030c64ce72b4e4605cb01f2ba405b7d",
"name": "Arclight", // need to display this information
"showtimes": {
"b4c2c326a4d335da654d4fd944bf88d0": [ // need to use this id
"11:30 pm", "2:45 pm", "8:35 pm", "4:15 pm", "10:30 pm"
]
}
}]
// movies.json
[{
"b4c2c326a4d335da654d4fd944bf88d0": { // to retrieve the title, rating, and poster
"title": "Fifty Shades Darker", // needs to be displayed
"rating": "R", // needs to be displayed
"poster": "https://dl.dropboxusercontent.com/s/dt6wgt92cu9wqcr/fifty_shades_darker.jpg" // needs to be displayed
}
}]
我有可以检索剧院的title 和name 的服务。但现在我必须使用showtimes 对象中的值来显示正确的标题名称。如您所见,b4c2c326a4d335da654d4fd944bf88d0 是电影标题的 id,可以从 movies.json 文件中检索到。
到目前为止,这是我的组件
ngOnInit() {
this._moviesDataService.getShowtimes()
.subscribe(res => this.results = res)
}
这是我的服务。
getShowtimes (): Observable<ShowTimes> {
return this._http.get<ShowTimes>(this._showtimesURL)
}
我的问题是如何使用电影的 id 检索电影的 title?这需要两个链式 Observable 吗?我需要遍历电影数组和.filter 吗?
我已经包含了一个我正在尝试构建的示例
【问题讨论】:
-
是的,你需要组合 observables。查看例如
forkJoin和各种map方法。
标签: angular typescript rxjs observable angular2-observables