おはようございます。私はAngular2の初心者で、私が取り組んでいるプロジェクトでは、RESTバックエンドからデータを取得するためにいくつかのHTTPリクエストを作成する必要があります。Angular2 + RxJSネストされたHTTPコール
バックエンドから、私はモジュールのリストを取得する必要があります。各モジュールには1つ以上のアクションがあります。 2つのエンティティを表すtypescriptですクラスは、バックアップされたが、相対的なアクションと与えられたモジュールのためのアクションのリストを取得するための第2のエンドポイントなしのモジュールのリストを取得するための呼び出しを公開
export class Module {
public name: string;
public htmlPath: string;
public actions: ModuleAction[];
constructor(data: IModule){
this.name = data.name;
this.actions = [];
this.htmlPath = data.htmlPath;
}
addAction(action: ModuleAction): void {
this.actions.push(action);
}
}
export class ModuleAction {
private name: string;
constructor(data: IModuleAction){
this.name = data.actionName;
}
}
以下の通りです。私の角度のインターフェイスを初期化するために、モジュールリストを取得するための最初のHTTPリクエストを作成する必要があります。モジュールを取得すると、それぞれのアクションを取得してModuleインスタンスを更新するための2つ目のリクエストをそれぞれ作成する必要があります。
昨日、RxJSを使用してデータを取得しようとしました。私は私が間違ってRxJS演算子を使用していますし、より多くの「対応RxJS」で同じ結果を得るために、さまざまなソリューションが存在しなければならないことはかなり確信している
getModules(type?: string) : Observable<Module[]> {
let url = ...
console.log("getting modules");
return this.http.get(url)
.map(res => {
let body = res.json();
let modules = body.map(jsModule => {
let m = new Module(jsModule);
let url = HttpHandlerService.URL_INFO + '/' + m.htmlPath;
console.log("getting actions for module " + m.name);
this.http.get(url)
.map(response => {
let body = response.json();
return body.actions.map(function(a : IModuleAction){
return new ModuleAction(a);
});
})
.subscribe(actions => {
for(let a of actions)
m.addAction(a);
});
return m;
});
return modules;
})
.catch(this.handleError);
}
次されていた唯一の作業溶液方法。
同じ結果を得るためにRxJS演算子を使用するにはどうすればよいですか? ありがとうございました。
どのようなエラーが表示されますか? –
@VinayPandya私は何の誤りもありません。私はちょうど同じことをするRxJS演算子を使用してより良い方法があるのだろうかと疑問に思っている – hara
http://stackoverflow.com/documentation/rxjs/8247/common-recipes/27973/sending-multiple-parallel-httpを見てください-requests#t = 20170316111633998635またはhttp://stackoverflow.com/documentation/rxjs/8247/common-recipes/28035/sending-multiple-sequential-http-requests#t=201703161116455216797 – martin