簡體   English   中英

等待 observable 的值(作為列表)使用列表中的每個成員調用另一個 observable?

[英]Wait for an observable's value (as a list) to call another observable using each member from the list?

我正在為我的應用程序使用 3 個端點,其中 1 個端點取決於另一個端點的響應,即一個列表,然后我需要使用列表中的每個項目才能使用另一個端點,我們稱它們為epAepBepCepA返回一個列表,然后我在epB上使用這個列表,類似於epA.Foreach( x => epB(x))並且我試圖將epBepC組合成一個組合列表,因為它們共享相似的字段。

我的問題是,我使用 angular 和 observables 太新了,我不知道是否有辦法將這些 epB 和 epC 結果結合起來(不提目前,我訂閱了 observables 並將其值分配給我需要的其他對象)...如果有人可以幫我一把,將不勝感激。 抱歉,如果這太亂了,我在 angular 中發帖和編碼的經驗很少。

這是我目前擁有的一些代碼......它有點難看,但它可以完成工作

代碼

this._serverRequests.epA(this._Token).subscribe(x => {
    this.servers = x;
    x.forEach(server => 
      this._serverRequests.epB(server)
      .subscribe(info => {
         this.serverInfo = info;
         this.GridModel.data = info['States'];
         this.GridModel.data.forEach(se => {
             se.Start = this.formatValuesPipe.transform(se.Start, 'grid');
         });
         this.GridModel.data.map( o => {
            o.ServerUrl = server;
         });
       })
     );
},
   error => this.errMsg = <any>error
);

this._serverRequests.epC(this._Token).subscribe(lic => {
   this.licensesList = lic;
   this.licensesModel.data = this.licensesList.LicenseUsageList;
   this.licensesModel.data.forEach(li => {
      li.AcquisitionTime = this.formatValuesPipe.transform(li.AcquisitionTime, 'grid'); 
   });
});

我也試過forkjoin,但由於epA返回一個列表,我不知道如何調用forkjoin中的每個項目

您可以將第一次調用的響應 map 轉換為調用數組,然后使用 combineLatest 給出所有響應的數組。

this._serverRequests.epA(this._Token).pipe
  map(server => this._serverRequests.epB(server)),
).subscribe(requests => {
  combineLatest(requests).subscribe(results => {
    // You have an array of the multiple results here
  })
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM