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

Commit 9693204

Browse files
topherfangioThomasBurleson
authored andcommitted
fix(autocomplete): fix md-not-found bug with multiple autocompletes
If a developer used more than one autocomplete on the page, all autocompletes after the first which included a md-not-found template would incorrectly assume that they also had one even if they did not. Fix logic to reset the variable to `false` when necessary. Fixes #5400. Closes #5442.
1 parent fbf45fd commit 9693204

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,39 @@ describe('<md-autocomplete>', function() {
372372
$timeout.flush();
373373
element.remove();
374374
}));
375+
376+
it('properly sets hasNotFound with multiple autocompletes', inject(function($timeout, $material) {
377+
var scope = createScope();
378+
var template1 =
379+
'<md-autocomplete' +
380+
' md-selected-item="selectedItem"' +
381+
' md-search-text="searchText"' +
382+
' md-items="item in match(searchText)"' +
383+
' md-item-text="item.display"' +
384+
' placeholder="placeholder">' +
385+
' <md-item-template>{{item.display}}</md-item-template>' +
386+
' <md-not-found>Sorry, not found...</md-not-found>' +
387+
'</md-autocomplete>';
388+
var element1 = compile(template1, scope);
389+
var ctrl1 = element1.controller('mdAutocomplete');
390+
391+
var template2 =
392+
'<md-autocomplete' +
393+
' md-selected-item="selectedItem"' +
394+
' md-search-text="searchText"' +
395+
' md-items="item in match(searchText)"' +
396+
' md-item-text="item.display"' +
397+
' placeholder="placeholder">' +
398+
' <md-item-template>{{item.display}}</md-item-template>' +
399+
'</md-autocomplete>';
400+
var element2 = compile(template2, scope);
401+
var ctrl2 = element2.controller('mdAutocomplete');
402+
403+
// The first autocomplete has a template, the second one does not
404+
expect(ctrl1.hasNotFound).toBe(true);
405+
expect(ctrl2.hasNotFound).toBe(false);
406+
}));
407+
375408
});
376409

377410
describe('xss prevention', function() {

src/components/autocomplete/js/autocompleteDirective.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,8 @@ function MdAutocomplete () {
153153
leftover = element.html(),
154154
tabindex = attr.tabindex;
155155

156-
if (noItemsTemplate) {
157-
hasNotFoundTemplate = true;
158-
}
156+
// Set our variable for the link function above which runs later
157+
hasNotFoundTemplate = noItemsTemplate ? true : false;
159158

160159
if (attr.hasOwnProperty('tabindex')) {
161160
element.attr('tabindex', '-1');

0 commit comments

Comments
 (0)