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的可观察值
我这里有两个问题:
将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?