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

Commit 96cfa74

Browse files
devversionThomasBurleson
authored andcommitted
fix(autocomplete): fix not found template detection when element is hidden
When the element is hidden at compilation time through a ng-if directive for example. The stored variable as currently used will be trashed, so it will be undefined. So there a possibilities to store that state, using a variable in the directive (ex. hashmaps with ids) or storing it as an attribute / class. Fixes #7035 Fixes #7142 Closes #7042
1 parent 7803395 commit 96cfa74

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,33 @@ describe('<md-autocomplete>', function() {
531531
element.remove();
532532
}));
533533

534+
it('properly sets hasNotFound when element is hidden through ng-if', inject(function() {
535+
var scope = createScope();
536+
var template1 =
537+
'<div>' +
538+
'<md-autocomplete ' +
539+
'md-selected-item="selectedItem" ' +
540+
'md-search-text="searchText" ' +
541+
'md-items="item in match(searchText)" ' +
542+
'md-item-text="item.display" ' +
543+
'placeholder="placeholder" ' +
544+
'ng-if="showAutocomplete">' +
545+
'<md-item-template>{{item.display}}</md-item-template>' +
546+
'<md-not-found>Sorry, not found...</md-not-found>' +
547+
'</md-autocomplete>' +
548+
'</div>';
549+
var element = compile(template1, scope);
550+
var ctrl = element.children().controller('mdAutocomplete');
551+
552+
expect(ctrl).toBeUndefined();
553+
554+
scope.$apply('showAutocomplete = true');
555+
556+
ctrl = element.children().controller('mdAutocomplete');
557+
558+
expect(ctrl.hasNotFound).toBe(true);
559+
}));
560+
534561
it('properly sets hasNotFound with multiple autocompletes', inject(function($timeout, $material) {
535562
var scope = createScope();
536563
var template1 =

src/components/autocomplete/js/autocompleteDirective.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,20 @@ function MdAutocomplete () {
149149
inputId: '@?mdInputId'
150150
},
151151
link: function(scope, element, attrs, controller) {
152-
controller.hasNotFound = element.hasNotFoundTemplate;
153-
delete element.hasNotFoundTemplate;
152+
// Retrieve the state of using a md-not-found template by using our attribute, which will
153+
// be added to the element in the template function.
154+
controller.hasNotFound = !!element.attr('md-has-not-found');
154155
},
155156
template: function (element, attr) {
156157
var noItemsTemplate = getNoItemsTemplate(),
157158
itemTemplate = getItemTemplate(),
158159
leftover = element.html(),
159160
tabindex = attr.tabindex;
160161

161-
// Set our variable for the link function above which runs later
162-
element.hasNotFoundTemplate = !!noItemsTemplate;
162+
// Set our attribute for the link function above which runs later.
163+
// We will set an attribute, because otherwise the stored variables will be trashed when
164+
// removing the element is hidden while retrieving the template. For example when using ngIf.
165+
if (noItemsTemplate) element.attr('md-has-not-found', true);
163166

164167
// Always set our tabindex of the autocomplete directive to -1, because our input
165168
// will hold the actual tabindex.

0 commit comments

Comments
 (0)