Autocomplete should always handle display value and model separately #14086
Labels
area: material/autocomplete
feature
This issue represents a new feature or feature request rather than a bug or bug fix
needs: discussion
Further discussion with the team is needed before proceeding
P3
An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Bug, feature request, or proposal:
Feature request / proposal
What is the expected behavior?
Assuming the following:
TS
HTML
I expect
text
to be of typestring
at any time. When selecting an option from the dropdown, I do not expect that value to be assigned to the input value. MatAutocomplete should provide a separatengModel
to be bound to. Sure, the selected option should be displayed in the input, but using astring
. This is possible via the[displayWith]
input of<mat-autocomplete>
, but should not be an "opt-in" feature, it should be required.What is the current behavior?
When you select an option,
text
is now of type{name: string}
, because Angular Material autocomplete assigns the selected value to it.What are the steps to reproduce?
What is the use-case or motivation for changing an existing behavior?
When you fetch search results from a backend, you're usually subscribing to
valueChanges
(if usingFormControl
) orngModelChange
(if usingngModel
). You would expect such subscriptions to stream values of the same type at runtime.In the docs, you have worked around this problem by doing the following:
Which works fine, but is not intuitive, and semantically awkward. The docs aren't clear about this specific semantic inconsistency which may cause users (like me) to waste some time trying to understand why typing is not preserved until realizing it's not a bug, but an intended behavior.
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Angular 7
Is there anything else we should know?
The text was updated successfully, but these errors were encountered: