AsyncPipe works with BehaviorSubject, but I don't want to initialize my service with empty data, for this reason I use Subject intead.
The problem is NgFor and asyncPipe doesn't work with Subject, is it an issue?
This works:
Component:
export class AppComponent {
foo = new BehaviorSubject<Array<number>>([1,2,3]);
getFoo(){
return this.foo.asObservable();
}
}
Template
<span *ngFor="let e of foo.asObservable() | async">{{e}}</span>
This NOT WORKS:
Component
export class AppComponent {
foo = new Subject<Array<number>>();
constructor(){
setTimeout(() => {
this.foo.next([1,2,3]);
}, 3000);
}
getFoo(){
return this.foo.asObservable();
}
}
Template
<span *ngFor="let e of foo.getFoo() | async">{{e}}</span>