【问题标题】:How to listen data changing in angular (with rx.js)如何监听角度变化的数据(使用 rx.js)
【发布时间】:2017-06-14 17:16:15
【问题描述】:

我正在从后端获取数据

我的服务

  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;
        }    
      )
  }

}

例如,后端更改了一些数据。 如何在我的组件中收听它?

【问题讨论】:

标签: angular typescript stream rxjs observable


【解决方案1】:

您可以使用 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;
        })
  }

【讨论】:

  • 它调用后端?我想像在 websockets 中一样听。
猜你喜欢
  • 2019-08-11
  • 2012-05-26
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
相关资源
最近更新 更多