2016-05-23 64 views
1

我想我需要某種類型的諾言鏈,但語法躲開我結果...角2 - 兩個服務,第二個要求的第一

在同一組件:

我打電話:

this.somethingService.getSomethings().then(somethings => this.somethings = somethings); 

然後我需要調用:

this.otherService.getOthers(this.somethings).then(others => this.others = others); 

在第二服務呼叫我使用的結果Ø f第一個在其內容上執行集合函數,但在第二個調用時它是空的,因此第二個服務返回空。

如何獲得第二個服務,以等待第一個承諾解決。

感謝名單

史蒂夫

回答

1

你可以鏈的承諾是這樣的:

this.somethingService.getSomethings().then(somethings => { 
    this.somethings = somethings; 
    return this.otherService.getOthers(somethings); 
}).then(others => { 
    this.others = others; 
}); 

第二個回調將接收第一回調的承諾回報的結果。

+0

感謝您的快速響應,這不會錯誤 - 但this.somethings是空的? – Steve

+0

Oups。我的回答中有一個錯字:'somethings'而不是'this.somethings'。我更新了它... –

+1

好吧,Service 2現在返回計算值,但由於* ngIf =「somethings.length> 0」,我的視圖是空的 - 它仍然認爲它是一個空數組。 – Steve

相關問題