我的應用程序的幾個部分中的一個非常常見的功能是接受字符串然後根據字符串過濾表的搜索框。在angularJs中編程搜索文本框的正確方法
我的目標是允許用戶鍵入某些內容,並在幾ms延遲後自動更新結果。即不需要點擊輸入按鈕。
數據的當然排序將使用AngularJs過濾器完成。然而,在我們更新過濾器之前,我相信我們首先必須瞭解用戶已經完成輸入並正在等待結果。
因此,我制定了將附加到搜索輸入框的指令。
<input type="text" update-filter placeholder="Enter search term">
//and the directive goes like this
app.directive('updateFilter', [ '$timeout', function($timeout){
var delay;
return {
link: function(scope, element){
element.on('keypress', function(){
if(!delay) {
delay = $timeout(function() {
//perform the activity of updating the filter here
delay = undefined;
},50);
}
}
}
}
});
我的問題是,這是正確的做法採取解決這個問題還是有更好的解決方案了嗎?
這隻有在客戶端擁有所有數據時纔有效。 – CodesInChaos 2015-06-16 13:53:39
@CodesInChaos - true,它只會過濾加載到應用程序中的數據。 – 2015-06-16 20:55:11