angular / angular/components

Autocomplete should always handle display value and model separately

オープン
#14,086 コメント 4 件 リアクション 21 件 担当者 0 名 GitHub で見る
area: material/autocomplete feature needs: discussion P3
主要言語
TypeScript
スター
25k
フォーク
6.8k
平均マージ
1日 8時間
マージ済み PR(30日)
91

説明

#### 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?

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

autocomplete の概要にある「setting separate control and display values」セクションと、displayWith および ngModel の動作を含む mat-autocomplete のエントリポイントから始めます。何かを変更する前に、提案された API と Angular 7 との互換性を明確にします。完了とは、選択されたオプションが文字列として表示され、バインドされたテキスト値は文字列のままになり、文書化された動作が直感的になることを意味します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
angular, typescript
領域
frontend
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。