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.
pipeAngular pipe which takes a rxjspipe()(the static one from the rxjs entry point) as its argument and similarly to the async pipe takes care of dealing with when the input observable changes. Then use it likedata$ | pipe: yourPipes | asyncwithyourPipes = pipe(tap(...))- Ingo Bürk$obs, and assigning the output ofthis.myService.getData()tothis.obs$? Or it that just a typo in the post? - R. Richardsplaindata to the children. - Hafnernuss