Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 定期更新角度2中的可观测值_Javascript_Events_Angular_Observable - Fatal编程技术网

Javascript 定期更新角度2中的可观测值

Javascript 定期更新角度2中的可观测值,javascript,events,angular,observable,Javascript,Events,Angular,Observable,我想从http链接每隔5秒点击并显示一次,而且似乎使用angular2,一个可观察到的将是一种方式 getPhaseVotes(issue: string) { return this.http.get(this.URL + 'issue/' + issue + '/getPhaseVotes') .subscribe(data => this.phase_votes = data.json(), err => conso

我想从http链接每隔5秒点击并显示一次,而且似乎使用angular2,一个可观察到的将是一种方式

getPhaseVotes(issue: string) {
    return this.http.get(this.URL + 'issue/' + issue + '/getPhaseVotes')
        .subscribe(data => this.phase_votes = data.json(),
                   err => console.log(err),
                   () => this.getStatus(issue));
}

我应该如何每5秒更新一次?

您可以使用
可观察的
操作符的
interval

@Injeactable()
export class SomeService {
  constructor(private http:Http) {}

  getPhaseVotes(issue: string) {
    return Observable.interval(5000).flatMap(() => {
      return this.http.get(this.URL + 'issue/' + issue + '/getPhaseVotes')
        .map(res => res.json());
    });
  }
}
这样,您需要调用
getPhaseVotes
方法并订阅它。每5秒,HTTP请求将透明执行,并在订阅的回调中提供结果:

@Component({
  (...)
})
export class SomeComponent {
  constructor(private service:SomeService) {
    this.service.getPhaseVotes('someissue')
      .subscribe(data => this.phase_votes = data,
               err => console.log(err),
               () => this.getStatus(issue));
  }
}
本文可以在其“轮询”部分为您提供更多提示:


您可以使用
可观察的
间隔
操作符:

@Injeactable()
export class SomeService {
  constructor(private http:Http) {}

  getPhaseVotes(issue: string) {
    return Observable.interval(5000).flatMap(() => {
      return this.http.get(this.URL + 'issue/' + issue + '/getPhaseVotes')
        .map(res => res.json());
    });
  }
}
这样,您需要调用
getPhaseVotes
方法并订阅它。每5秒,HTTP请求将透明执行,并在订阅的回调中提供结果:

@Component({
  (...)
})
export class SomeComponent {
  constructor(private service:SomeService) {
    this.service.getPhaseVotes('someissue')
      .subscribe(data => this.phase_votes = data,
               err => console.log(err),
               () => this.getStatus(issue));
  }
}
本文可以在其“轮询”部分为您提供更多提示: