angular / angular/components

bug(MatAutocomplete): MatAutocomplete + FormControl does not clear input correctly after option selection

未关闭
#25,809 4 条评论 5 个 reaction 已指派 0 人 在 GitHub 查看
area: material/input P3
主要语言
TypeScript
星标
25k
派生
6.8k
平均合并
1 天 8 小时
30 天内合并 PR
91

描述

### Is this a regression?

- [ ] Yes, this behavior used to work in the previous version

### The previous version in which this bug was not present was

_No response_

### Description

See example: https://material.angular.io/components/chips/examples#chips-autocomplete

In this example, after selecting an autocomplete option, we want to create a new chip and clear the typed text in the input element. The input is bound to a FormControl instance, so it should be possible to clear the input by simply calling `this.fruitCtrl.setValue(null)` or `this.fruitCtrl.setValue('')`. Instead, the previously typed text remains visible, but if you subscribe to the `FormControl.valueChanges` property you will see the new empty value emitted correctly.

The example works around this issue by accessing the native input element and updating the value directly.

### Reproduction

Steps to reproduce:
1. See example https://material.angular.io/components/chips/examples#chips-autocomplete
2. Remove the nativeElement reference
3. Input text remains visible despite the FormControl instance correctly emitting the new value, creating an inconsistent state

### Expected Behavior

It should be enough to just update the FormControl to ensure the input element has the correct value set.

### Actual Behavior

The input element is left in an inconsistent state unless the user manually updates the native element's value directly.

### Environment

- Angular: n/a (internal stable version http://go/ng-cli)
- CDK/Material: n/a
- Browser(s): Chrome
- Operating System (e.g. Windows, macOS, Ubuntu): macOS

贡献指南

打开贡献指南

调研方向

从 issue 中链接的 chips-autocomplete 示例开始,使用 MatAutocomplete 和 FormControl 重现该行为。跟踪选项选择后 control 的空值如何传递到输入框;完成标准是仅更新 FormControl 就能清除可见文本,而无需直接访问 nativeElement。

由索引模型根据 Issue 内容生成。

评估

技术栈
angular, typescript
领域
frontend
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。