如何将 locationId 数组传递给服务 getLocationData?

How to pass array of locationId to service getLocationData?

如何将 locationId 数组传递给服务?

我有一组位置 ID

locationArr=[40871, 60009, 38149, 40868, 43240, 15299, 53897, 40976, 38151, 23183, 38152, 78579, 23180, 40977, 23176, 39565, 40884, 15298, 38147, 40966, 39669] 

实际上我需要将 locationArr 传递给 http://192.168.7.45:9200/location/_doc/+locationArr

我需要将 locationArr 上存在的 locationId 数组传递给服务,以便为数组 locationArr 上的每个 locationId 获取 GPS1 纬度和经度。

service get location by locationId for only a locationId but for array of location 这是我的问题

getLocationData(id: number) {  
console.log("server "+id)  
return this.http.get('http://192.168.7.45:9200/location/_doc/'+id);  
}  

所以请问如何在位置数组内循环实现它

calling service
this.partDetailsService.getLocationData(this.LocationId).subscribe(res => {          
         this.dataLocation = res['_source']['GPS1'];     
         var loc = this.dataLocation.split(',');      
         this.lat = loc[0].trim();    
         this.lng = loc[1].trim(); 

首先你的后端需要更改为接受多个locationId,好像只接受一个,然后基于它你可以发送你的数据。

如果您的后端将支持多个 ID 作为 GET 中的逗号分隔值,那么您的 angular 代码将如下所示

getLocationData(ids: Array<number>) {  
   console.log("server "+ids)  
   return this.http.get('http://192.168.7.45:9200/location/_doc/'+ids.join(','));  
} 

如果您的后端将支持多个 ID 作为 POST 正文数组,那么您的 angular 代码将如下所示

getLocationData(ids: Array<number>) {  
   console.log("server "+ids)  
   return this.http.post('http://192.168.7.45:9200/location/_doc/', { ids: ids });  
}  

如果你的后端不支持多个id,那么你需要循环遍历id并为每个id调用后端

getLocationData(ids: Array<number>) {  
    let observableBatch = [];

    this.ids.forEach((id) => {

        observableBatch.push(
              this.http.get('http://192.168.7.45:9200/location/_doc/'+id)
                       .map((res) => res.json()));
    });

    return Observable.forkJoin(observableBatch);
}


this.partDetailsService.getLocationData(this.LocationIds).subscribe(res : Array<any> => {  
         since it's an array you needs to loop through res        
         res.forEach((item, index) => {
            const dataLocation = res[index]['_source']['GPS1'];     
            const loc = this.dataLocation.split(',');      
            this.lat[index] = loc[0].trim();    
            this.lng[index] = loc[1].trim();
         });
}

总而言之,后端api签名驱动怎么调用