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

Commit 3f0d686

Browse files
devversionThomasBurleson
authored andcommitted
fix(autocomplete): initialize scope variable with empty string
* Currently the autocomplete searchText will be initialized with the value of null, but this is not valid, because once the autocomplete will be focused the searchText will be set to to an empty string. In general the searchText should be initialized with an empty string, if the scope variable is not defined. Fixes #8767 Closes #8802
1 parent 145ce63 commit 3f0d686

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,40 @@ describe('<md-autocomplete>', function() {
626626
expect($mdUtil.enableScrolling).toHaveBeenCalled();
627627
}));
628628

629+
it('should initialize the search text with an empty string', inject(function($mdUtil, $timeout, $material) {
630+
var scope = createScope();
631+
632+
// Delete our searchText variable from the generated scope, because we
633+
// want to confirm, that the autocomplete uses an empty string by default.
634+
delete scope.searchText;
635+
636+
var template =
637+
'<md-autocomplete' +
638+
' md-selected-item="selectedItem"' +
639+
' md-search-text="searchText"' +
640+
' md-items="item in match(searchText)"' +
641+
' md-item-text="item.display"' +
642+
' placeholder="placeholder">' +
643+
' <md-item-template>{{item.display}}</md-item-template>' +
644+
' <md-not-found>Sorry, not found...</md-not-found>' +
645+
'</md-autocomplete>';
646+
var element = compile(template, scope);
647+
var ctrl = element.controller('mdAutocomplete');
648+
649+
$material.flushOutstandingAnimations();
650+
651+
// Run our initial flush
652+
$timeout.flush();
653+
waitForVirtualRepeat(element);
654+
655+
// Set our search text to a value that we know doesn't exist
656+
expect(scope.searchText).toBe('');
657+
658+
// Make sure we wrap up anything and remove the element
659+
$timeout.flush();
660+
element.remove();
661+
}));
662+
629663
it('ensures the parent scope digests along with the current scope', inject(function($timeout, $material) {
630664
var scope = createScope(null, {bang: 'boom'});
631665
var template =

src/components/autocomplete/js/autocompleteController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function MdAutocompleteCtrl ($scope, $element, $mdUtil, $mdConstant, $mdTheming,
6363
* Initialize the controller, setup watchers, gather elements
6464
*/
6565
function init () {
66-
$mdUtil.initOptionalProperties($scope, $attrs, { searchText: null, selectedItem: null });
66+
$mdUtil.initOptionalProperties($scope, $attrs, { searchText: '', selectedItem: null });
6767
$mdTheming($element);
6868
configureWatchers();
6969
$mdUtil.nextTick(function () {

0 commit comments

Comments
 (0)