2016-11-03 5 views
0

検索機能は正常に動作しますが、入力された文字ごとにサービスにヒットします。だから、私は2つの入力レターの間に3秒を超えるギャップがある場合、サービスにヒットしたい。検索機能2文字の違いが3秒を超えるとサービスが起動する

どうすれば実装できますか?

+0

人々はどこまであなたを見ることができるように、」、ここにあなたのコードを投稿してください。行って助けてください。オプションを絞り込む必要があります。 http://stackoverflow.com/tourとhttp://stackoverflow.com/help/how-to-askをお読みください。 – Tom

答えて

0

まず、あなたのコンポーネントで定義:

var search$: Subject<string> = new BehavioralSubject<string>(''); 

を次の検索、更新を行う時:

searchUpdated(value: string){ 
    this.search$.next(value); 
} 

次にあなたが見ることができるあなたのngInit

this.search$.debounce(3000).subscribe(searchTerm => this.searchService.update(searchTerm)); 
0
this.search(term: string) 
.debounce(3000)    
.filter(term => term.length > 2) 
.subscribe(); 

に追加これは参考用ですサービスコールのための https://github.com/Reactive-Extensions/RxJS

+0

私が 'dobounce'を使用すると、サービスの応答でエラーが発生します – Madhu

+0

あなたのコードスニペットを共有してください。 – Manish

0

GetSearchResult(searchKey:any) 
    { 
     this.timer = setTimeout(() => { 
      this.service.GetSearchItems(searchKey) 
      .subscribe(
       response =>{ 
       this.TableResponse = response; 
      }); 
     }, 3000); 
    } 

clearTimer() 
{ 
    if(this.timer != null) 
    clearTimeout(this.timer); 
} 

のOnChange私は意志setTimeOutとするとき、タイムアウト​​イベントの前に押されたキーがトリガーとclearTimeOut

関連する問題