我想我可以结合 Angular 中的示例来获得 dropdown和 Input 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>
哪个接近预期的方式
清除输入。我现在面临的问题是它立即打开选择框。无论如何如何防止这种行为?
我知道还有其他清除选择的解决方案。我想知道这种方法是否可行?
最佳答案
(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/