Skip to content

Commit 675c9df

Browse files
crisbetotinayuangao
authored andcommitted
fix(autocomplete): "undefined" being displayed on empty control with ngModel (#3535)
* fix(autocomplete): "undefined" being displayed on empty control with ngModel Fixes "undefined" being displayed as the autocomplete input's value, if the value is undefined and the input uses `ngModel`. Fixes #3529. * const
1 parent fe315ef commit 675c9df

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
292292
}
293293

294294
private _setTriggerValue(value: any): void {
295-
this._element.nativeElement.value =
296-
this.autocomplete.displayWith ? this.autocomplete.displayWith(value) : value;
295+
const toDisplay = this.autocomplete.displayWith ? this.autocomplete.displayWith(value) : value;
296+
this._element.nativeElement.value = toDisplay || '';
297297
}
298298

299299
/**

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {MdAutocompleteModule, MdAutocompleteTrigger} from './index';
55
import {OverlayContainer} from '../core/overlay/overlay-container';
66
import {MdInputModule} from '../input/index';
77
import {Dir, LayoutDirection} from '../core/rtl/dir';
8-
import {FormControl, ReactiveFormsModule} from '@angular/forms';
8+
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
99
import {Subscription} from 'rxjs/Subscription';
1010
import {ENTER, DOWN_ARROW, SPACE, UP_ARROW} from '../core/keyboard/keycodes';
1111
import {MdOption} from '../core/option/option';
@@ -26,9 +26,14 @@ describe('MdAutocomplete', () => {
2626
dir = 'ltr';
2727
TestBed.configureTestingModule({
2828
imports: [
29-
MdAutocompleteModule.forRoot(), MdInputModule.forRoot(), ReactiveFormsModule
29+
MdAutocompleteModule.forRoot(), MdInputModule.forRoot(), FormsModule, ReactiveFormsModule
30+
],
31+
declarations: [
32+
SimpleAutocomplete,
33+
AutocompleteWithoutForms,
34+
NgIfAutocomplete,
35+
AutocompleteWithNgModel
3036
],
31-
declarations: [SimpleAutocomplete, AutocompleteWithoutForms, NgIfAutocomplete],
3237
providers: [
3338
{provide: OverlayContainer, useFactory: () => {
3439
overlayContainerElement = document.createElement('div');
@@ -863,6 +868,18 @@ describe('MdAutocomplete', () => {
863868
}).not.toThrowError();
864869
});
865870

871+
it('should display an empty input when the value is undefined with ngModel', async(() => {
872+
const fixture = TestBed.createComponent(AutocompleteWithNgModel);
873+
874+
fixture.detectChanges();
875+
876+
fixture.whenStable().then(() => {
877+
const input = fixture.debugElement.query(By.css('input')).nativeElement;
878+
879+
expect(input.value).toBe('');
880+
});
881+
}));
882+
866883
it('should work when input is wrapped in ngIf', () => {
867884
const fixture = TestBed.createComponent(NgIfAutocomplete);
868885
fixture.detectChanges();
@@ -997,6 +1014,36 @@ class AutocompleteWithoutForms {
9971014

9981015
}
9991016

1017+
1018+
@Component({
1019+
template: `
1020+
<md-input-container>
1021+
<input mdInput placeholder="State" [mdAutocomplete]="auto" [(ngModel)]="selectedState"
1022+
(ngModelChange)="onInput($event)">
1023+
</md-input-container>
1024+
1025+
<md-autocomplete #auto="mdAutocomplete">
1026+
<md-option *ngFor="let state of filteredStates" [value]="state">
1027+
<span>{{ state }}</span>
1028+
</md-option>
1029+
</md-autocomplete>
1030+
`
1031+
})
1032+
class AutocompleteWithNgModel {
1033+
filteredStates: any[];
1034+
selectedState: string;
1035+
states = ['New York', 'Washington', 'Oregon'];
1036+
1037+
constructor() {
1038+
this.filteredStates = this.states.slice();
1039+
}
1040+
1041+
onInput(value: any) {
1042+
this.filteredStates = this.states.filter(s => new RegExp(value, 'gi').test(s));
1043+
}
1044+
1045+
}
1046+
10001047
/**
10011048
* Focuses an input, sets its value and dispatches
10021049
* the `input` event, simulating the user typing.

0 commit comments

Comments
 (0)