Angular 7 : Clear selection with x-button on a dropdown box

标签 angular angular-material angular7

我想我可以结合 Angular 中的示例来获得 dropdownInput with clear box到:

<mat-form-field >
  <mat-select placeholder="Country" [(value)]="selectedCountry" (selectionChange)="emitItemChanges()">
    <div *ngFor="let item of lstItems|async">
      <mat-option *ngIf="addItem(item)" [value]="item">{{item.title}}</mat-option>
    </div>
  </mat-select>
  <button mat-button *ngIf="selectedCountry" matSuffix mat-icon-button aria-label="Clear" 
    (click)="selectedCountry=undefined">
    <mat-icon>close</mat-icon>
  </button>
</mat-form-field>

哪个接近预期的方式

enter image description here

清除输入。我现在面临的问题是它立即打开选择框。无论如何如何防止这种行为?

我知道还有其他清除选择的解决方案。我想知道这种方法是否可行?

最佳答案

(click)="selectedCountry=undefined; $event.stopPropagation()" 有帮助!感谢@Sachila :-)

完整代码如下:

  <button mat-button *ngIf="selectedCountry" matSuffix mat-icon-button 
    aria-label="Clear" (click)="selectedCountry=undefined; $event.stopPropagation()">
  <mat-icon>close</mat-icon>

关于 Angular 7 : Clear selection with x-button on a dropdown box,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53555741/

相关文章:

Angular Header/Http/RequestOptions 自 angular 6.0 以来已弃用,如何将它们更新到 angular 8.0?

angular - 如何使用 RxJS 和 Angular2 构建真正的可观察集合?

angular - 使用 rxjs .subscribe 和 Angular 防止末日金字塔 - 压平 .subscribes 的数量

Angular 4 : ERROR TypeError: Cannot read property 'length' of undefined while accessing length property

css - 在预建的 Angular Material 主题上更改字体

node.js - Angular 8 : Dynamically set formControlName in html based on component

angular - 如何在 Angular 6 中导入 ag-grid 主题

html - Mat Select 多个设置选定值

Angular 7 错误 RangeError : Maximum call stack size exceeded

javascript - 操作第三次运行时未触发效果