i have a quick question regarding Angular 4 and the Observable. In my program i have a Component, which creates a list of "things" that should be send to an api. Secondly, i have a service, which gets an element ("thing" --> one!), sends it to the api and returns a Observable. Here´s the code: Component:
saveTimers() {
// gehe alle selektierten Timer durch und lege Sie an
let timerSuccess = [];
let timerFailed = [];
let timers = [];
for (let epgEntry of this.searchResult) {
if (epgEntry.checked) {
let timer = new Timer;
// magic
this.timerService.saveTimer(timer).
subscribe(
data => {
console.log(data);
if (data) {
timerSuccess.push(timer);
} else {
timerFailed.push(timer);
}
},
error => { this.variables.setFailure(error); }
);
}
}
//both empty, because api didn´t return yet
console.log(timerSuccess);
console.log(timerFailed);
if (timerSuccess.length > 0) {
this.variables.setSuccess('Timer erfolgreich angelegt');
}
if (timerFailed.length > 0) {
this.variables.setFailure('Timer konnte(n) nicht angelegt werden');
}
}
Service:
saveTimer(timer: Timer): Observable<boolean> {
// magic --> returns an observable
}
Now the question: how can i use Observable.forkJoin() (in the component) to do ALL api calls at one time (btw wait for all api calls)?
Thanks so much!