Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 7f35a3f

Browse files
feat(typeahead): support wait-ms option
1 parent db366a8 commit 7f35a3f

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/typeahead/test/typeahead.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,16 @@ describe('typeahead tests', function () {
326326
$timeout.flush();
327327
expect($scope.isLoading).toBeFalsy();
328328
}));
329+
330+
it('should support timeout before trying to match $viewValue', inject(function ($timeout) {
331+
332+
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in source | filter:$viewValue' typeahead-wait-ms='200'></div>");
333+
changeInputValueTo(element, 'foo');
334+
expect(element).toBeClosed();
335+
336+
$timeout.flush();
337+
expect(element).toBeOpenWithActive(1, 0);
338+
}));
329339
});
330340

331341
describe('selecting a match', function () {

src/typeahead/typeahead.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
2929
};
3030
}])
3131

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) {
3333

3434
var HOT_KEYS = [9, 13, 27, 38, 40];
3535

@@ -42,6 +42,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
4242
//minimal no of characters that needs to be entered before typeahead kicks-in
4343
var minSearch = originalScope.$eval(attrs.typeaheadMinLength) || 1;
4444

45+
//minimal wait time after last character typed before typehead kicks-in
46+
var waitTime = originalScope.$eval(attrs.typeaheadWaitMs) || 0;
47+
4548
//expressions used by typeahead
4649
var parserResult = typeaheadParser.parse(attrs.typeahead);
4750

@@ -124,12 +127,23 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
124127
//$parsers kick-in on all the changes coming from the view as well as manually triggered by $setViewValue
125128
modelCtrl.$parsers.push(function (inputValue) {
126129

130+
var timeoutId;
131+
127132
resetMatches();
128133
if (selected) {
129134
return inputValue;
130135
} else {
131136
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+
}
133147
}
134148
}
135149

0 commit comments

Comments
 (0)