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

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

reflection - How to access the object-members of an object declaration in kotlin -

php - Doctrine Query Builder Error on Join: [Syntax Error] line 0, col 87: Error: Expected Literal, got 'JOIN' -