我从后端获取数据
我的服务
getItalianTeams() {
return this.http.get("https://raw.githubusercontent.com/opendatajson/football.json/master/2016-17/it.111.clubs.json")
.map(result => result.json());
}
我的订阅者
ngOnInit() {
this.teamService.getItalianTeams()
.subscribe (
(data) => {
this.teams = data.clubs;
}
)
}
}
例如,一些数据在后端发生了变化。 我怎样才能在我的组件中听到它?
答案 0 :(得分:0)
您可以使用websocket,或者您可以通过调用api来调查api,如下所示(将每隔5秒更新一次):
ngOnInit() {
Observable
.interval(5000) /* milliseconds between calls */
.startWith(0) /* first call immediately */
.switchMap(() => this.teamService.getItalianTeams())
.subscribe((data) => {
this.teams = data.clubs;
})
}