【发布时间】:2018-09-06 07:10:46
【问题描述】:
我们以下面的英雄代码获取英雄为例:
/** GET heroes from the server */
getHeroes (): Observable<Hero[]> {
return this.http.get<Hero[]>(this.heroesUrl)
.pipe(
tap(heroes => this.log(`fetched heroes`)),
catchError(this.handleError('getHeroes', []))
);
}
我想操作 hero[] 的每个元素。我试过 .map 功能:
getHeroes (): Observable<Hero[]> {
return this.http.get<Hero[]>(this.heroesUrl)
.pipe(
tap(heroes => this.log(`fetched heroes`)),
catchError(this.handleError('getHeroes', []))
).map((result) => {
console.log(result); // I am getting all the array and not iteration.
return Object.assign(new Hero(), result);
);
}
但是我得到了所有的数组或 Hero[] 并且它没有一个一个地迭代英雄。
【问题讨论】:
-
把“地图”放在“点击”之前,见stackoverflow.com/questions/47275385/…
-
您需要在发出的数组上调用 Array.map(),而不是在 observable 上。顺便说一句,你真的不应该混合使用管道运算符和旧的实例运算符。坚持使用可管道化的运营商。
-
map(result=>{return result.map(x=>Object.assign(new Hero(),x)})
标签: angular