angular / angular/components

Autocomplete should always handle display value and model separately

Ouverte
#14,086 4 commentaires 21 réactions 0 personnes assignées Voir sur GitHub
area: material/autocomplete feature needs: discussion P3
Langage dominant
TypeScript
Étoiles
25k
Forks
6.8k
Merge moyen
1 j 8 h
PR mergées (30 j)
91

Description

#### Bug, feature request, or proposal:

Feature request / proposal

#### What is the expected behavior?

Assuming the following:

**TS**

```typescript
options = [{name: 'One'}, {name: 'Two'}];
text: string;
```

**HTML**

```html


{{option.name}}

```

I expect `text` to be of type `string` 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 separate `ngModel` to be bound to. Sure, the selected option should be displayed in the input, but using a `string`. This is possible via the `[displayWith]` input of ``, 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 using `FormControl`) or `ngModelChange` (if using `ngModel`). You would expect such subscriptions to stream values of the same type at runtime.

In [the docs](
https://material.angular.io/components/autocomplete/overview#setting-separate-control-and-display-values), you have worked around this problem by doing the following:

```
map(value => typeof value === 'string' ? value : value.name),
```

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?

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Commencez par la section « setting separate control and display values » de la vue d’ensemble d’autocomplete et par le point d’entrée de mat-autocomplete, notamment le comportement de displayWith et de ngModel. Clarifiez l’API proposée et sa compatibilité avec Angular 7 avant de modifier quoi que ce soit ; le travail doit être considéré comme terminé lorsque l’option sélectionnée est affichée sous forme de chaîne, tandis que la valeur texte liée reste une chaîne, et que le comportement documenté devient intuitif.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
angular, typescript
Domaine
frontend
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.