Angular2 Observables立即映射和订阅?

时间:2017-03-22 20:41:40

标签: angular dictionary observable subscribe

我有一个订阅这样的observable的函数:

fieldType\W+select(?:(?!conjunction).){1}field.*type\W+radio$\r?\n?

这样我可以两次调用该函数。我试过像

这样的东西
public whatever(): Observable<Response> {
  this.someService.functionReturnsObservable(params)
    .subscribe(
      success => {
        /* stuff to be done after observable's next */
      }
    );

  return this.someService.functionReturnsObservable(params)
    .maps((response: Response) => response));
}

1 个答案:

答案 0 :(得分:4)

只有订阅时才会运行observable。如果它返回.map,但没有订阅它,它将永远不会运行.map。尝试订阅map

的结果
public whatever(): Observable<Response> {
  let returnObs = this.someService.functionReturnsObservable(params)
    .map((response: Response) => { 
      console.log('map');
    });

  returnObs.subscribe((response) => {
      console.log('subscribe');
    });

  return returnObs;
}