@@ -29,7 +29,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
29
29
} ;
30
30
} ] )
31
31
32
- . directive ( 'typeahead' , [ '$compile' , '$parse' , '$q' , '$document' , '$position' , 'typeaheadParser' , function ( $compile , $parse , $q , $document , $position , typeaheadParser ) {
32
+ . directive ( 'typeahead' , [ '$compile' , '$parse' , '$q' , '$timeout' , '$ document', '$position' , 'typeaheadParser' , function ( $compile , $parse , $q , $timeout , $document , $position , typeaheadParser ) {
33
33
34
34
var HOT_KEYS = [ 9 , 13 , 27 , 38 , 40 ] ;
35
35
@@ -42,6 +42,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
42
42
//minimal no of characters that needs to be entered before typeahead kicks-in
43
43
var minSearch = originalScope . $eval ( attrs . typeaheadMinLength ) || 1 ;
44
44
45
+ //minimal wait time after last character typed before typehead kicks-in
46
+ var waitTime = originalScope . $eval ( attrs . typeaheadWaitMs ) || 0 ;
47
+
45
48
//expressions used by typeahead
46
49
var parserResult = typeaheadParser . parse ( attrs . typeahead ) ;
47
50
@@ -124,12 +127,23 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
124
127
//$parsers kick-in on all the changes coming from the view as well as manually triggered by $setViewValue
125
128
modelCtrl . $parsers . push ( function ( inputValue ) {
126
129
130
+ var timeoutId ;
131
+
127
132
resetMatches ( ) ;
128
133
if ( selected ) {
129
134
return inputValue ;
130
135
} else {
131
136
if ( inputValue && inputValue . length >= minSearch ) {
132
- getMatchesAsync ( inputValue ) ;
137
+ if ( waitTime > 0 ) {
138
+ if ( timeoutId ) {
139
+ $timeout . cancel ( timeoutId ) ; //cancel previous timeout
140
+ }
141
+ timeoutId = $timeout ( function ( ) {
142
+ getMatchesAsync ( inputValue ) ;
143
+ } , waitTime ) ;
144
+ } else {
145
+ getMatchesAsync ( inputValue ) ;
146
+ }
133
147
}
134
148
}
135
149
0 commit comments