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

Commit 3b76647

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 c59f33e commit 3b76647

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
@@ -488,6 +488,33 @@ describe('<md-autocomplete>', function() {
488488
element.remove();
489489
}));
490490

491+
it('properly sets hasNotFound when element is hidden through ng-if', inject(function() {
492+
var scope = createScope();
493+
var template1 =
494+
'<div>' +
495+
'<md-autocomplete ' +
496+
'md-selected-item="selectedItem" ' +
497+
'md-search-text="searchText" ' +
498+
'md-items="item in match(searchText)" ' +
499+
'md-item-text="item.display" ' +
500+
'placeholder="placeholder" ' +
501+
'ng-if="showAutocomplete">' +
502+
'<md-item-template>{{item.display}}</md-item-template>' +
503+
'<md-not-found>Sorry, not found...</md-not-found>' +
504+
'</md-autocomplete>' +
505+
'</div>';
506+
var element = compile(template1, scope);
507+
var ctrl = element.children().controller('mdAutocomplete');
508+
509+
expect(ctrl).toBeUndefined();
510+
511+
scope.$apply('showAutocomplete = true');
512+
513+
ctrl = element.children().controller('mdAutocomplete');
514+
515+
expect(ctrl.hasNotFound).toBe(true);
516+
}));
517+
491518
it('properly sets hasNotFound with multiple autocompletes', inject(function($timeout, $material) {
492519
var scope = createScope();
493520
var template1 =

src/components/autocomplete/js/autocompleteDirective.js

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

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

161164
if (!attr.hasOwnProperty('tabindex')) element.attr('tabindex', '-1');
162165

0 commit comments

Comments
 (0)