I'd like to provide an rxjs Subject
from an Angular service to be able to emit values (via next
) by calling methods on the service. One of the values I want it to emit is the result of an Angular HttpClient
get
call. I just can't seem to get it right. I am wondering why the following results in the subscribe handler not being called:
-View
export default abstract class TileView implements OnInit {
constructor (private configService : ConfigService) {}
ngOnInit () {
this.configService.fetch(this.type()).subscribe( res => {
console.log(res)
});
}
}
-Service
export class ConfigService {
public subject = new AsyncSubject();
constructor (private http : HttpClient) {}
fetch (type) {
this.http.get(
api.host + api.base + interpolate(api.config, { type }) + "/"
).subscribe( res => {
this.subject.next(res);
});
return this.subject;
}
}
Is there any way to return the subject and also fire off the http call with a single method call? It's strange because the subject is returned, a subscriber is registered, the http call completes and this.subject.next(res)
is called but the subscribe handler doesn't even run.
fetch
from the View which I show there. Thesubscribe
is happening there. – papirofetch (type) { this.http.get( api.host + api.base + interpolate(api.config, { type }) + "/" ).subscribe( res => { return res });
– Farasi78