2017-12-15 11 views
0

ユーザーの入力が空でなく、何らかの遅延があってユーザーが入力を完了したときにのみサービスを呼び出したいとします。入力が空でなく、入力が完了するまでの遅延がある場合の接尾語

以下はコードです。助けてください。

Component.ts

searchByName(event) { 

this.facilityService.searchFacilityName(event.target.value).subscribe(施設=> this.facilities =設備)。あなたが待機する時間のすなわち量後の最初の時間制限を指定する必要が

+0

あなたが角度反応性のフォームを使用していますか? – Dimuthu

+0

はい私は反応的な企業を使用しています –

答えて

0

これは参考になる、

this.testForm.controls.searchText.valueChanges.subscribe(x=>{ 
    if(this.testForm.controls.searchText.dirty && this.testForm.controls.searchText.valid) { 
    setTimeout(function(){ 
     this.searchFacilityName(this.testForm.controls.searchText.value) 
    },3000); 
    } }); 
1

}

Service.ts

searchFacilityName(name) { 
return this.http.get('http://localhost:8000/searchFacilityByName/' + name) 
    .map((response:Response) => response.json()) 
    .catch(
    (error: Response) => { 
     return Observable.throw('Something went wrong. Please try again.') 
    } 
); 

}ユーザーが入力を停止したことを確認する最後のkeyupイベント。 その後、あなたのコンポーネントでは、この

<input type='text' [ngModel]='userInput' (keyup)='checkTimeLimit()' (keydown)='typeCheck = typeCheck + 1'> 

を試すことができます。

typeCheck = 0; 
userInput; 
timeLimit = 5000; // It is the time limit after which you consider that user has stopped typing (5s) 

checkTimeLimit() { 
    const temp = this.typeCheck; 
    setTimeout(() => { 
    if (temp === this.typeCheck) { // It checks whether the user has pressed another key before the specified time limit 
     this.typeCheck = 0; 
     this.searchFacilityName(userInput); 
    } 
    }, this.timeLimit); 
} 
関連する問題