i am working on an Angular 5 application and i have an API that store my data. The thing is i have two components, one for displaying data and one for posting data.
They both interact with the API through a service with two functions:
addCandidate(candidate: Candidate): Observable<Candidate> {
this.candidatesList.next(this.http.get(this.apiUrl));
return this.http.post<Candidate>(this.apiUrl, candidate, httpOptions);
}
listCandidates(): Observable<Candidate[]> {
return this.http.get(this.apiUrl).map(res => <Candidate[]>res);
}
The components have methods for subscribing to the service:
saveCandidate(): void {
if(this.candidate.skills.indexOf(', ') >= 0) {
this.candidate.skills = (<string>this.candidate.skills).split(', ');
}
this.candidatesService.addCandidate(this.candidate).subscribe();
}
And respectively ( this one is on init ) :
ngOnInit() {
this.candidatesService.listCandidates().subscribe(candidates => this.candidates = candidates);
}
So, hence i can't really know when the api is updated, i want to update the list after saveCandidate() method takes action.
How does one update one component with new api data after another submitted to the servic?.
Side note: i tried using rxjs Subject.asObservable().next(..the http request). But Observable.next is not a method. Maybe i misused them.