Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Observable.forkJoin rxJs请求在另一个请求中_Angular_Typescript_Rxjs - Fatal编程技术网

Angular Observable.forkJoin rxJs请求在另一个请求中

Angular Observable.forkJoin rxJs请求在另一个请求中,angular,typescript,rxjs,Angular,Typescript,Rxjs,我想以某种方式向我的服务器发送两个请求,以获取对象列表(DocumentComponent),在这个DocumentComponent中,我有一个对象DocumentContents列表 所以我需要在每次我得到一个对象DocumentComponent之后,我也得到它的DocumentContents this.gridService.getAllDocumentComponentByTemplate(1) .subscribe(data => {

我想以某种方式向我的服务器发送两个请求,以获取对象列表(DocumentComponent),在这个DocumentComponent中,我有一个对象DocumentContents列表

所以我需要在每次我得到一个对象DocumentComponent之后,我也得到它的DocumentContents

this.gridService.getAllDocumentComponentByTemplate(1)
            .subscribe(data => {
                    this.documentComponents = data;
                    console.log(this.documentComponents);
                    for (var component of this.documentComponents) {
                           this.getAllDocumentContents(component.id);
                    }
                },
                error => alert('Erreur ' + error),
                () => {
                    console.log("finished  ");
                }
            );
但这种方法并不能并行工作 我只得到最新DocumentComponent的最后一个DocumentContent列表

那么如何使用Observable.forkJoin或者如果有其他方法,谢谢

 Observable.forkJoin(
            this.gridService.getAllDocumentComponentByTemplate(1),
            this.gridService.getAllDocumentContentByDocumentComponent(**id**)
        ).subscribe(data => {
            this.documentComponents = data[0];
            this.documentContents = data[1];
        },
            err => console.error(err)
    );
我试过了,但是我得到了DocumentContents的最新列表,因为我在第二个方法getAllDocumentContentByDocumentComponent中将其作为id,作为静态值,我需要将其设置为变量

谢谢你抽出时间

getAllDocumentContentByDocumentComponent(idDocumentComponent:number) {
        return this._http.get('http://localhost:8080/template/getAllDocumentContentByDocumentComponent/' + idDocumentComponent)
            .map(res => res.json());
    }
    getAllDocumentComponentByTemplate(idDocument:number) {
        return this._http.get('http://localhost:8080/template/getAllDocumentComponentByTemplate/' + idDocument)
            .map(res => res.json());
    }

我想试试这样的东西:

this.gridService.getAllDocumentComponentByTemplate(1)
    .flatMap(documentComponents => {
      let contentObservables =  documentComponents.map(documentComponent => {
        return this.gridService.getAllDocumentContentByDocumentComponent(
          documentComponent.id);
      });

      return Observable.forkJoin([
        Observable.of(documentComponents),
        ...contentObservables
      ]);
    })
首先获取文档组件,构建可观察对象以获取每个组件的内容,然后使用
forkJoin
并行执行所有操作


注意使用
Observable.of
来获得初始请求的结果。

什么是
id
以及从之前的请求中获得它?谢谢id是DocumentComponent的DocumentContent的外部id。根据此id,我可以访问当前DocumentComponent的所有DocumentContent。好的
getAllDocumentComponentByTemplate
返回
DocumentComponent
的列表?还是一个?没错,它会返回一个列表我会更新我的问题