I am successfully subscribing to an observable in a couple of different components in my Angular/Ionic app. However, because I am doing so manually, that also means I need to manually unsubscribe to them, which I currently do in my ngOnDestroy()
life cycle hook.
The component code implementation I have, that IS working, looks like this:
getPlayerStats() {
this.playerSubscription = this.mainService.getPlayerStats(this.username).subscribe(
user => {
this.user = user;
},
error => this.errorMsg = error
);
}
My view code for the component looks like this:
<ion-card *ngIf="user">
... display user info
</ion-card>
And the service method that is called here looks like this:
getPlayerStats(username: string) {
const req = `users/stats/${username}`;
return this.apiService.sendRequest(req);
}
What I'd like to do, but am having difficulty getting to work, is using the async
pipe do the subscribing in my HTML view. That'd be cleaner because then I wouldn't have to be so verbose and I wouldn't have to manually subscribe and unsubscribe in the components where I use this. My understanding is that this is also the recommended way of handling these scenarios, for precisely these reasons.
What would that code look like? I tried this but it did not work:
getPlayerStatus() {
this.user = this.mainService.getPlayerStats(this.username);
}
And in my HTML view I did this:
<ion-card *ngIf="user | async">
... display user info
</ion-card>
But as I say, this isn't working. What do I need to change here? How can I call the service getPlayerStats()
method, but handle the subscribing in the component view with the async pipe?