My angular2 app has a feature where it adds or removes items from a multiple select box and sends each item to a remote api individually for processing in the database. The method below is used to send the request to the server and receive a response as an observable:
removeFromClientLibrary(username: string, roleID: string): Observable<any> {
let body = JSON.stringify({Key: username, Value: roleID});
return this._http.post(this._removeFromLibraryURL, body, this.emsConfig.getStandardHttpHeaders())
.map((res:Response) => <any>res.json());
}
I have set up a client to consume this by using the subscribe method. I initially thought that calling subscribe once should suffice, but for some reason it doesn't work that way. The only way I could get the desired result was to loop through each item and subscribe multiple times like so:
for (let i = 0; i < selected.length; i++) {
this._accountsService.removeFromClientLibrary(this.username, selected[i].RoleID)
.subscribe(status => {
console.log(status);
});
}
I'm wondering if there's a more efficient way to handle the API results using rxjs without having to subscribe multiple times as shown above. Is there another approach to fetching the data?