【发布时间】:2020-07-04 18:21:45
【问题描述】:
我有一个带有“电影”模型的 API。这部电影与一个流派相关联。当我从 API 获取电影时,我将流派的 url 作为“流派”属性获取。 在我的代码中,我正在创建这样的电影:
fetchMovies(token: string){
this.httpOptions = {
headers: new HttpHeaders({
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json',
})
};
return this.http.get<{[key: string]: MovieData}>(this.url, this.httpOptions).pipe(
map(res => {
const movies = [];
for (const key in res) {
if (res.hasOwnProperty(key)) {
movies.push(
new Movie(
key,
res[key].title,
res[key].desc,
res[key].year,
this.getGenre(res[key], res[key].genre),
res[key].thumbnail,
res[key].movie,
res[key].createdUser,
res[key].premium));
}
}
return movies;
}),
tap(movies => {
this._movies = movies;
})
);
}
getGenre() 方法应该将流派作为字符串返回。
我尝试了以下方法:
getGenre(key, url): string{
let result;
this.http.get(url, this.httpOptions).subscribe(
(res: any) => {
result = res.title;
}
);
return result;
}
我怎样才能做到这一点?
我愿意接受任何想法。
【问题讨论】:
标签: angular ionic-framework rxjs