简体   繁体   English

angular 材料 mat-select with mat-chip-list 通过 ENTER 而不是 DELETE 或 BACKSPACE 删除芯片

[英]angular material mat-select with mat-chip-list remove chip by ENTER instead of DELETE or BACKSPACE

I have a component that contains a mat-select and displays the values selected in the mat-chip-list.我有一个包含 mat-select 并显示 mat-chip-list 中选择的值的组件。 I am currently working on accessability of the component.我目前正在研究组件的可访问性。 One of the requirements I received is to allow mat-chip removal using the Enter in place or in addition to Delete and Backspace .我收到的要求之一是允许使用Enter原位或除DeleteBackspace之外的方式移除垫屑。 I checked the documentation of Angular Material and what I found is just that:我检查了 Angular Material 的文档,我发现只是: 在此处输入图像描述 remove消除
Allows for programmatic removal of the chip.允许以编程方式移除芯片。 Called by the MatChipList when the DELETE or BACKSPACE keys are pressed.当按下 DELETE 或 BACKSPACE 键时由 MatChipList 调用。 Informs any listeners of the removal request.通知删除请求的所有侦听器。 Does not remove the chip from the DOM.不从 DOM 中移除芯片。

<mat-form-field>
  <mat-label>Choose Colors</mat-label>
  <mat-select [formControl]="colorsControl" multiple>

    <mat-option *ngFor="let color of colorsList" [value]="color">{{color}}</mat-option>
  </mat-select>
</mat-form-field>
    <div>
      <mat-chip-list>
        <mat-chip *ngFor="let color of colorsControl.value"
          [removable]="true" (removed)="onColorRemoved(color)" (keydown.enter)="onColorRemoved(color)">
          {{ color }}
          <mat-icon matChipRemove>cancel</mat-icon>
        </mat-chip>
      </mat-chip-list>
    </div>
<br/>

StackBlitz demo StackBlitz 演示

Any help will be appreciated.任何帮助将不胜感激。

In the end I found a simple solution to my problem: By adding keydown event on mat-chip:最后,我找到了解决问题的简单方法:通过在 mat-chip 上添加 keydown 事件:

 <mat-chip *ngFor="let color of colorsControl.value"
          [removable]="true" (removed)="onColorRemoved(color)" (keydown.enter)="onColorRemoved(color)">
          {{ color }}
          <mat-icon matChipRemove>cancel</mat-icon>
        </mat-chip>

StackBlitz solution demo StackBlitz 解决方案演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 材料可重复使用 mat-chip-list 与 mat-autocomplete - Angular material reusable mat-chip-list with mat-autocomplete Angular Material 6 Mat-Chip-List-两个Mat-chip-list声明共享同一数据源 - Angular Material 6 Mat-Chip-List - two mat-chip-list declarations share the same data source Angular 2+ material mat-chip-list formArray 验证 - Angular 2+ material mat-chip-list formArray validation 输入未显示验证错误的角形材料垫芯片列表 - Angular material mat-chip-list with input not showing validation error Angular - 无法使用集合和 *ngFor 填充 mat-chip-list - 更新:设置 mat-select multiple 的值有问题吗? - Angular - Cannot populate mat-chip-list with collection and *ngFor - Updated: Set value of mat-select multiple is problem? Angular mat-chip-list 带文本区怎么用? - How to use Angular mat-chip-list with text area? Angular 材料(如果块的高度很大,则切割 mat-chip-list )。 Android上的唯一 - Angular Material ( mat-chip-list cutted if height of block is big ). The only on Android 垫芯片列表中的角材料ngFor-如何防止断线? - Angular Material ngFor in mat-chip-list - how to prevent from line-break? 'mat-chip-list' 不是已知元素 - 'mat-chip-list' is not a known element 如何以表格形式发送 mat-chip-list - how to send mat-chip-list in form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM