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

Commit b58c9c8

Browse files
feat(typeahead): support typeahead-loading bindable expression
Closes #321
1 parent 467afcd commit b58c9c8

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/typeahead/test/typeahead.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,21 @@ describe('typeahead tests', function () {
311311
changeInputValueTo(element, 'not in matches');
312312
expect($scope.result).toEqual(undefined);
313313
});
314+
315+
it('should bind loading indicator expression', inject(function ($timeout) {
316+
317+
$scope.isLoading = false;
318+
$scope.loadMatches = function(viewValue) {
319+
return $timeout(function() { return [];}, 1000);
320+
};
321+
322+
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in loadMatches()' typeahead-loading='isLoading'></div>");
323+
changeInputValueTo(element, 'foo');
324+
325+
expect($scope.isLoading).toBeTruthy();
326+
$timeout.flush();
327+
expect($scope.isLoading).toBeFalsy();
328+
}));
314329
});
315330

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

src/typeahead/typeahead.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ angular.module('ui.bootstrap.typeahead', [])
3030
}])
3131

3232
//options - min length
33-
.directive('typeahead', ['$compile', '$q', '$document', 'typeaheadParser', function ($compile, $q, $document, typeaheadParser) {
33+
.directive('typeahead', ['$compile', '$parse', '$q', '$document', 'typeaheadParser', function ($compile, $parse, $q, $document, typeaheadParser) {
3434

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

@@ -49,6 +49,8 @@ angular.module('ui.bootstrap.typeahead', [])
4949
//should it restrict model values to the ones selected from the popup only?
5050
var isEditable = originalScope.$eval(attrs.typeaheadEditable) !== false;
5151

52+
var isLoadingSetter = $parse(attrs.typeaheadLoading).assign || angular.noop;
53+
5254
//create a child scope for the typeahead directive so we are not polluting original scope
5355
//with typeahead-specific data (matches, query etc.)
5456
var scope = originalScope.$new();
@@ -64,6 +66,7 @@ angular.module('ui.bootstrap.typeahead', [])
6466
var getMatchesAsync = function(inputValue) {
6567

6668
var locals = {$viewValue: inputValue};
69+
isLoadingSetter(originalScope, true);
6770
$q.when(parserResult.source(scope, locals)).then(function(matches) {
6871

6972
//it might happen that several async queries were in progress if a user were typing fast
@@ -88,8 +91,12 @@ angular.module('ui.bootstrap.typeahead', [])
8891
} else {
8992
resetMatches();
9093
}
94+
isLoadingSetter(originalScope, false);
9195
}
92-
}, resetMatches);
96+
}, function(){
97+
resetMatches();
98+
isLoadingSetter(originalScope, false);
99+
});
93100
};
94101

95102
resetMatches();

0 commit comments

Comments
 (0)