Disclaimer: this is closely related to this question.
Currently I have two components, Parent
and Child
.
The Parent
passes an Observable to the Child
, which subscribes to this Observable in the template to avoid lifecycle/memory leak issues. But I have to pipe
the Observable in the Child
component to set up a few things.
parent.ts:
public $obs = Observable<any>;
....
loadData() {
this.obs$ = this.myService.getData();
}
parent.html:
<child [data$]="obs$"></child>
child.ts:
@Input() data$ : Observable<any>;
ngOnChanges(changes) {
if(changes.data$) {
console.log("changes detected");
this.data$.pipe(tap(data => console.log("tap worked")));
}
}
child.html
<div *ngIf="data$ | async as data;">
{{ data || json }}
</div>
The data is present in the template, the console log shows that the changes have been detected, but the "tap worked" is never shown, leading me to suspect that maybe my call to pipe
comes too late.
Is there any way to pipe before the template "calls" subscribe?
The idea is having many components similiar to child, all doing different things on the data object. I thought about subscribing to the service in the Parent
and passing the json values directly to all childs, but that would require me to write up an *ngIf
in all childs.