Angular Template: How to bind RXJS Observable and read its properties?

Lior Kooznits picture Lior Kooznits · Jun 7, 2016 · Viewed 22.8k times · Source

I have created this interface:

interface IGame {
    name: string;
    description: string;
}

I'm using it as an Observable and passing it as Input to the Component:

@Input() public game: Observable<IGame>;

I can see its value printed using the JSON pipe:

 <h3>{{game | json}}</h3>

When binding to a specific property, nothing is displayed (just an empty string):

 <h3>{{game.name}}</h3>
 <h3>{{game.description}}</h3>

Answer

G&#252;nter Z&#246;chbauer picture Günter Zöchbauer · Jun 7, 2016

The async pipe does the subscription in view bindings

 <h3>{{(game | async)?.name}}</h3>

The ? is only necessary when null values might be emitted.