我有一个角度组件 使用像这样的构造函数:
newTrips$: Observable<Trip[]>;
constructor(private store: Store<fromRoot.State>, private tripFeedService: TripFeedService) {
this.newTrips$ = store.select(fromRoot.getTripFeedUnreadTrips);
this.newTrips$.subscribe(x => { console.log('subscribe'); console.log(x); });
}
在视图中我有这个:
<div *ngFor="let trip of newTrips$ | async">test</div>
问题是如果页面加载,Trip []为空。视图中的ngfor永远不会被调用。我的控制台会显示显示&#39; subscribe&#39;的日志消息。和列表中的对象。如果刷新页面,则会显示项目,并且可在控制台和ui中正确显示observable的所有后续更改
答案 0 :(得分:0)
由于您在async
中使用ngFor
,因此无需订阅构造函数中的observable。
newTrips$: Observable<Trip[]>;
constructor(private store: Store<fromRoot.State>, private tripFeedService: TripFeedService) {
this.newTrips$ = store.select(fromRoot.getTripFeedUnreadTrips);
}