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

拜拜、爱过 提交于 2019-12-21 12:10:20

问题


I thought I can combine the examples from Angular for dropdown and the Input with clear box to:

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

Which works close to the way as intended

which clears the input. The problem I'm facing now is that it immediately opens the selection box. Anyway how to prevent this behaviour?

I know there exists other solutions for clearing the selection. I want to know if this approach is possible?


回答1:


(click)="selectedCountry=undefined; $event.stopPropagation()" helped! Thx to @Sachila :-)

So the full code looks like:

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


来源:https://stackoverflow.com/questions/53555741/angular-7-clear-selection-with-x-button-on-a-dropdown-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!