So I have this method that returns some observable.
fetch(publication: Publication): Observable<Array<ProcessingCenter>> {
return this.http.get(`${BASE_URL}/api/publications/${publication.id}`, {
headers: headers
}).map(response => {
return response.json() as Array<ProcessingCenter>;
})
}
I store returned value (getObservable always returns the same observable).
And in one component I utilize this observable in component:
<tr *ngFor="let pc of getObservable(p) | async">
...
</tr>
And now, even though I'm always using the same observable, when I toggle component, each time it is created and async pipe is subscribing, an AJAX request is issued. AFAIK AJAX request should be issued only once, when first subscription is done, right?
EDIT
Thanks for answers, but they just don't focus on the real issue. Let's me rephrase it.
Suppose I have a observable variable
myObservablereturned byHttpserviceSuppose in template I'm using this variable with
asyncpipeSuppose that I'm toggling a part of the template that is using
asyncpipe with simple*ngIf
E.g.:
<button (click)="toggle = !toggle">Toggle</button>
<table class="table table-condensed" *ngIf="toggle">
<!-- Somewhere in here is myObservable | async -->>
</table>
Now, when I click the button, each time table is toggled, http request is sent (even though myObservable is still the same observable object (component's instance field).
And the questions is: what is so special about async pipe or observable that is returned by Http service that makes them do Http request each time a subscription is made?