diff --git a/src/components/autocomplete/js/autocompleteController.js b/src/components/autocomplete/js/autocompleteController.js index 665dab4381f..194f9845186 100644 --- a/src/components/autocomplete/js/autocompleteController.js +++ b/src/components/autocomplete/js/autocompleteController.js @@ -19,7 +19,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming, selectedItemWatchers = [], hasFocus = false, lastCount = 0, - promiseFetch = false; + fetchesInProgress = 0; //-- public variables with handlers defineProperty('hidden', handleHiddenChange, true); @@ -648,15 +648,16 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming, if ( !items ) return; items = $q.when(items); + fetchesInProgress++; setLoading(true); - promiseFetch = true; $mdUtil.nextTick(function () { items .then(handleResults) .finally(function(){ - setLoading(false); - promiseFetch = false; + if (--fetchesInProgress === 0) { + setLoading(false); + } }); },true, $scope); } @@ -715,6 +716,10 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming, } } + function isPromiseFetching() { + return fetchesInProgress !== 0; + } + function scrollTo (offset) { elements.$.scrollContainer.controller('mdVirtualRepeatContainer').scrollTo(offset); } @@ -722,7 +727,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming, function notFoundVisible () { var textLength = (ctrl.scope.searchText || '').length; - return ctrl.hasNotFound && !hasMatches() && (!ctrl.loading || promiseFetch) && textLength >= getMinLength() && (hasFocus || noBlur) && !hasSelection(); + return ctrl.hasNotFound && !hasMatches() && (!ctrl.loading || isPromiseFetching()) && textLength >= getMinLength() && (hasFocus || noBlur) && !hasSelection(); } /**