javascript - Angular2 Observable isn't triggered by async pipe -
consider simple snippet of angular2/rxjs/typescript
public rooms: observable<room[]>; constructor ( ... ) { this.rooms = this.inspectshipsubject .do(() => console.log('foo')) .switchmap(ship => this.roombyshipservice.getroomsbyship(ship)); //this.rooms.subscribe(); // <-- [1] }
here's template:
<some-component *ngfor="let room of rooms | async" [room]="room"></some-component>
it doesn't work ('foo' not printed , no data shows) unless uncomment line indicated [1]
. why doesn't template trigger observable execute automatically?
is there better way use blank subscribe?
ok, i'm not sure why, trick use behavioursubject
instead of subject
on this.inspectshipsubject
.
this works nicely , triggered template.
public rooms$: observable<room[]>; private inspectshipsubject: behaviorsubject<ship> = new behaviorsubject<ship>(null); constructor( ... ) { } ngoninit() { this.rooms$ = this.inspectshipsubject.switchmap(ship => this.roombyshipservice.getroomsbyship(ship)); } @input() set ship(ship: ship) { this.inspectshipsubject.next(ship); } ship(): ship { return this.inspectshipsubject.getvalue(); }
and template:
<some-component *ngfor="let room of (rooms | async)" [room]="room"></some-component>
Comments
Post a Comment