如何在组件中的服务返回的observable上映射rxjs运算符

时间:2017-10-01 05:10:15

标签: angular rxjs5 angular2-observables

export class AppComponent implements OnInit {

  movieList: Observable<any>;
  constructor(private AppService:AppService){

  }
  ngOnInit() {
      this.getMovies();
  }

  getMovies() {

    this.movieList = this.AppService.getMovies().map(movie => {
      return {
        name: movie.name
      }
    });


  this.movieList.subscribe(data => {
    debugger;
  })
  }
}

我想对observable this.movi​​eList执行Rxjs操作,但是当我订阅this.movi​​eList时,调试器中的Data返回undefined

1 个答案:

答案 0 :(得分:0)

问题是你的getMovies返回一个集合,在地图中你将它视为单个项目。如果你想到这一点,它应该wotk

this.movieList = this.AppService.getMovies().map(movies => movies.map(movie => { return { name: movie.name } }));