In Angular (v12) I've got a following component (using dummy variable names):
export class DataDetailsComponent {
data$: Observable<MyDataModel>;
constructor(dataService: DataService, route: ActivatedRoute, private router: Router) {
this.data$ =
combineLatest([dataService.getDataObservable(), router.events])
.pipe(
filter(([,event]) => event instanceof ActivationEnd),
tap(([data, event]) => console.log((<ActivationEnd>event).snapshot.queryParams['id'])),
map(([data, event]) => data.filter(c => c.id === (<ActivationEnd>event).snapshot.queryParams['id'])[0]),
tap(dataItem => console.log(dataItem))
);
this.data$.subscribe(); // console.logs don't work without this
}
}
And its template:
<div *ngIf="data$ | async as data; else loading">
<img [src]="data.url" [alt]="data.name">
</div>
<ng-template #loading>Loading...</ng-template>
Data is not being rendered, and the browser console is empty if I don't actually subscribe to the data$
. On the other hand, when I do place this.data$.subscribe();
, console gets correct output, but the view is still empty (hangs on Loading...).
Can anyone explain what's going on?
In the end, it was the router event that was not letting observable complete. Apparently, it works if I move navigation from template to .ts and subscribe to
ActivatedRoute.params
instead ofRouter.events
.So, the parent component gets:
instead of using
routerLink
in template:And then, on
DataDetailsComponent
i can do:Reasons why are beyond me at this point.