【发布时间】:2021-04-03 02:57:54
【问题描述】:
我像这样在我的服务中调用 API
getItems(): Observable<{}> {
return this.http.get(<path>)
.pipe(
pluck('items'),
);
}
以这种形状返回数据:
items: Object { key: value, key: value, key: value ...}
我在我的组件中订阅了这个
items$!: any;
constructor(private readonly service: Service) { }
ngOnInit(): void {
this.service.getItems().subscribe(items => this.items$ = items);
}
并像这样在我的模板中显示它们
<div *ngFor="let item of items$ | keyvalue">
<mat-card>
<mat-card-header>
{{item.key}}
</mat-card-header>
<mat-card-content>
{{item.value}}
</mat-card-content>
</mat-card>
</div>
但是,我真正想要的是每隔几秒左右一次循环显示这些项目,而不是一次显示所有项目。我还想只进行一次 http 调用来完成此操作。我环顾四周,发现了解决方案的一些细节,但是,它通常涉及至少将对象转换为数组,然后可能使用某种主题来观察订阅的原始 observable?有什么方法可以做到这一点?谢谢。
【问题讨论】:
-
您是否希望它们全部同时显示,但速度很慢(就像一个缓慢增长的数组)。还是要先显示一个,再显示下一个,再显示下一个(如幻灯片放映)?
-
像幻灯片一样
标签: angular typescript rxjs angular-httpclient angular-observable