Angular 2使用带有Http.get的Observable.debounce()

Jos*_*hik 5 observable angular

据我所知,Observable.debounce()可用于处理快速火表输入.由于Http GET也返回一个Observable,我想它有可能去掉快速的http请求吗?我尝试过,debounceTime()但似乎没有做任何事情.

public getStuff(p1, area:string, p2:string): Observable<number> { 
   return this.http.get(some_url) 
   .map(r => r.json()) 
   .debounceTime(10000) 
  .catch(this.handleError); 
};
Run Code Online (Sandbox Code Playgroud)

Thi*_*ier 8

debounceTime允许缓冲事件和只能处理一定量的时间之后的最后一个.

它在输入的上下文中很有用,但它应该在observable上定义,该observable触发事件而不是为HTTP请求创建的事件.

以下是与利用debounceTime运算符的输入关联的控件的示例:

@Component({
  (...)
  template: `
    <input [ngFormControl]="ctrl"/>
  `
})
export class MyComponent {
  constructor() {
    this.ctrl = new Control();
    this.ctrl.valueChanges
               .debounceTime(500)
               .distinctUntilChanged()
               .switchMap((value: string) => {
                 // Get data according to the filled value
                 return this.service.getData(entry);
               })
               .subscribe(data => {
                 // Update the linked list
                 this.list = data;
               });
  }
}
Run Code Online (Sandbox Code Playgroud)

本文也可能对您感兴趣:

根据micryks的评论,这是一个额外的链接: