2017-07-10 63 views
0

我在一个类中有两个函数,两个类都从API调用中获取数据。Angular2在另一个具有相同类的函数中调用函数,并在第二个函数中使用第一个函数属性

这里是第一功能

getQuotes(){ 
    this.quoteService.getQuotes() 
    .subscribe(
     quotes => { 
     this.quotCode = this.quotes.BasicDetails[0].QuotCode; 
     } 
     }); 
} 

二功能

getOption(){ 
    this.quoteService.getOptions() 
    .subscribe(
      options => { 
    }) 
} 

我想从第一功能

this.quotCode = this.quotes.BasicDetails[0].QuotCode; 

得到这一行数据,并基于this.quotCodegetOption()将有不同的参数传递,并会得到差异。反应也。因此,有必要使用this.quotCode在第二功能

我打电话getQuotes()constuctor()

任何人都可以请帮我吗? 谢谢

+0

@trichetriche你能帮忙吗? –

回答

1

RXJSmergeMap是您需要的操作符:这样,您将在第一个请求成功时执行第二个请求。

this.quoteService.getQuotes().mergeMap(quotes => { 
    // Do whatever you need to do with quotes 
    return this.quoteService.getOptions() 
}).subscribe(options => { 
    // Do whatever you need with options 
}); 
+0

好吧,让我试试 –

+0

感谢它的工作 –

+0

它的工作,但有时'getOptions'根本不加载,即使第一次加载 –

相关问题