Hi, this is something I already fixed but still don't understand why it happens, I hope someone can guide me:
- on a component I have the following observable:
protected combinedObservable$ = combineLatest([
this.observable1$, //this is basically a state that gets filled with an http request
this.myForm.get('myfield').valueChanges //a listener to a formControl change
]).pipe(
map(([httpResponse, controlValue]) =>{
const result = httpResponse?.filter(r => controValue.includes(r.id));
console.log({httpResponse, controlValue, result});
return result; //result is an array
}),
);
so then I go to the component's template:
<div *ngIf="(combinedObservable$ | async)?.length > 0">
obsValue: {{combinedObservable$ | async}} //this prints null
obsLength: {{(combinedObservable$ | async)?.length}} // this prints 0
condition: {{(combinedObservable$ | async)?.length > 0}} //this prints false
</div>
the issue is, when I first change the value of myfield and the map operation is triggered, on console I see that the result is an array with 1 element, but then on my page I see that the div with the ngIf and its content gets rendered but the content of the div has outdated data from the subscription (value is null, length is 0 and the exact same condition from the ngIf is false), and this keeps happening until I select a new value.
I read something about cold and hot observables and then I fixed it adding a shareReplay(1) to my combinedObservable$. But I still dont understand why the async pipe behaves different when rendering changes and when inside an ngIf