2017-07-10 69 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'根本不加載,即使第一次加載 –

相關問題