Skip to content

fix(autocomplete): error if panel is added asynchronously #7078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,9 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
}

private _setTriggerValue(value: any): void {
const toDisplay = this.autocomplete.displayWith ? this.autocomplete.displayWith(value) : value;
const toDisplay = this.autocomplete && this.autocomplete.displayWith ?
this.autocomplete.displayWith(value) :
value;

// Simply falling back to an empty string if the display value is falsy does not work properly.
// The display value can also be the number zero and shouldn't fall back to an empty string.
Expand Down
12 changes: 11 additions & 1 deletion src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,15 @@ describe('MatAutocomplete', () => {
}).toThrow(getMatAutocompleteMissingPanelError());
}));

it('should not throw on init, even if the panel is not defined', fakeAsync(() => {
expect(() => {
const fixture = TestBed.createComponent(AutocompleteWithoutPanel);
fixture.componentInstance.control.setValue('Something');
fixture.detectChanges();
tick();
}).not.toThrow();
}));

it('should hide the placeholder with a preselected form control value ' +
'and a disabled floating placeholder', fakeAsync(() => {
const fixture = TestBed.createComponent(AutocompleteWithFormsAndNonfloatingPlaceholder);
Expand Down Expand Up @@ -1821,10 +1830,11 @@ class AutocompleteWithNativeInput {


@Component({
template: `<input placeholder="Choose" [matAutocomplete]="auto">`
template: `<input placeholder="Choose" [matAutocomplete]="auto" [formControl]="control">`
})
class AutocompleteWithoutPanel {
@ViewChild(MatAutocompleteTrigger) trigger: MatAutocompleteTrigger;
control = new FormControl();
}


Expand Down