Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 05a08c8

Browse files
devversionThomasBurleson
authored andcommitted
fix(autocomplete): only handle results if it's an array or a promise
At the moment the autocomplete will handle the results wrong. So if we specify for example an empty JSON-Object, the autocomplete will handle the results as an async promise. Fixes #7074 Closes #7089
1 parent bbbe9e3 commit 05a08c8

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,36 @@ describe('<md-autocomplete>', function() {
261261
element.remove();
262262
}));
263263

264+
it('should not show a loading progress when the items object is invalid', inject(function() {
265+
var scope = createScope(null, {
266+
match: function() {
267+
// Return an invalid object, which is not an array, neither a promise.
268+
return {}
269+
}
270+
});
271+
272+
var template =
273+
'<md-autocomplete ' +
274+
'md-input-id="{{inputId}}" ' +
275+
'md-selected-item="selectedItem" ' +
276+
'md-search-text="searchText" ' +
277+
'md-items="item in match(searchText)" ' +
278+
'md-item-text="item.display" ' +
279+
'tabindex="3"' +
280+
'placeholder="placeholder">' +
281+
'<span md-highlight-text="searchText">{{item.display}}</span>' +
282+
'</md-autocomplete>';
283+
284+
var element = compile(template, scope);
285+
var ctrl = element.controller('mdAutocomplete');
286+
287+
scope.$apply('searchText = "test"');
288+
289+
expect(ctrl.loading).toBe(false);
290+
291+
element.remove();
292+
}));
293+
264294
it('should clear value when hitting escape', inject(function($mdConstant, $timeout) {
265295
var scope = createScope();
266296
var template = '\

src/components/autocomplete/js/autocompleteController.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -640,10 +640,11 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
640640
function fetchResults (searchText) {
641641
var items = $scope.$parent.$eval(itemExpr),
642642
term = searchText.toLowerCase(),
643-
isList = angular.isArray(items);
643+
isList = angular.isArray(items),
644+
isPromise = !!items.then; // Every promise should contain a `then` property
644645

645-
if ( isList ) handleResults(items);
646-
else handleAsyncResults(items);
646+
if (isList) handleResults(items);
647+
else if (isPromise) handleAsyncResults(items);
647648

648649
function handleAsyncResults(items) {
649650
if ( !items ) return;

0 commit comments

Comments
 (0)