Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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 通过阵列进行角度循环并使用forkJoin收集观测值_Angular_Typescript_Angular2 Observables - Fatal编程技术网

Angular 通过阵列进行角度循环并使用forkJoin收集观测值

Angular 通过阵列进行角度循环并使用forkJoin收集观测值,angular,typescript,angular2-observables,Angular,Typescript,Angular2 Observables,我有一个方法,它获取字符串数组(tagArray:string[])作为输入。现在我循环遍历这个数组,并对服务器执行搜索(通过POST),以检查标记是否已经在数据库中 如果标记存在,服务器将返回相应的标记对象,并将其存储在另一个数组(searchFilter:{tags:tag[]}[]) 如果不存在,则向服务器发出另一个POST请求,将丢失的标记存储在后端,然后,创建的标记对象将存储在searchFilter['tags']中 performTagMatching(tagArray: stri

我有一个方法,它获取字符串数组
(tagArray:string[])
作为输入。现在我循环遍历这个数组,并对服务器执行搜索(通过POST),以检查标记是否已经在数据库中

如果标记存在,服务器将返回相应的标记对象,并将其存储在另一个数组
(searchFilter:{tags:tag[]}[])

如果不存在,则向服务器发出另一个POST请求,将丢失的标记存储在后端,然后,创建的标记对象将存储在
searchFilter['tags']

performTagMatching(tagArray: string[]):any {
  for(let tagElem of tagArray) {
    // create search request for tag lookup
    const searchRequest: SearchRequest = { term: tagElem, operation: 'exact'};

    this.apiClientService.findTagByTitle(searchRequest).subscribe(
      (response) => {
        // Check tag search result for existence of tag
        if(response.length == 0) {
          // No tag found -> create tag and add to search tag list
          this.searchFilter['tags'].push(this.saveNewTag(tagElem));
        } else {
          // Tag found -> add to search tag list
          this.searchFilter['tags'].push(response[0]);
        }
      },
      (error) => {
        console.log(error);
      }
    );
  }
}
如何将其放入forkJoin中,以便返回forkJoin的可观察值

我这里有两个问题:

  • 我需要响应中的“tagElem”,在我离开for循环后,我无法再访问它

  • 我不知道如何将观测值推送到数组中,并返回给方法调用方


  • 将http调用与for循环分开,并将其作为单独的函数调用

    performTagMatching(tagArray: string[]): any {
        for (let tagElem of tagArray) {
            // create search request for tag lookup
            const searchRequest: SearchRequest = {
                term: tagElem,
                operation: 'exact'
            };
    
            this.callMactchFunc(tagElem)
        }
    }
    
    private callMactchFunc(tagElem) {
        this.apiClientService.findTagByTitle(searchRequest).subscribe(
            (response) => {
                // Check tag search result for existence of tag
                if (response.length == 0) {
                    // No tag found -> create tag and add to search tag list
                    this.searchFilter['tags'].push(this.saveNewTag(tagElem));
                } else {
                    // Tag found -> add to search tag list
                    this.searchFilter['tags'].push(response[0]);
                }
            },
            (error) => {
                console.log(error);
            }
        );
    }
    

    但我的问题仍然存在:如何将所有请求放入forkJoin中,并且在以后订阅forkJoin时仍然访问tagElem?