javascript - 使用按钮关闭下拉菜单 - Angular Material
我正在使用 Angular Material Design 中的 selectTo 下拉菜单。
这是我的代码:
<mat-form-field id="inputClick" appearance="outline" (click)="Valid()">
<mat-label>{{'GENERAL.TITLE' | translate}} *</mat-label>
<mat-select [formControl]="toppings" multiple>
<div class="drpInput">
<mat-form-field class="mat-form-field-fluid" appearance="outline">
<mat-label>{{'GENERAL.TITLE' | translate}} *</mat-label>
<input autocomplete="off" matInput [placeholder]="'GENERAL.TITLE' | translate"
(keyup)="onTextChange($event.target.value)">
</mat-form-field>
</div>
<div class="oprionSelect">
<mat-option (click)="selectedUser(item.id)" *ngFor="let item of users" [value]="item.id">
<label>{{ item.displayName | translate }} </label><span class="mar">({{item.userName}})</span>
</mat-option>
</div>
<mat-progress-bar *ngIf="loading" mode="indeterminate"></mat-progress-bar>
<div class="row justofy-content-center text-center m-auto col-md-12 col-sm-12 col-lg-12 col-xl-12 col-lg12">
<div class="col-md-4 col-sm-4 col-lg-4 col-xl-4 col-lg-4 right">
<button mat-button color="primary" (click)="next()" *ngIf="nextBtn">بعدی</button>
</div>
<div class="col-md-4 col-sm-4 col-lg-4 col-xl-4 col-lg-4" *ngIf="count!=0">
<button mat-button (click)="close()" color="warn">انتخاب</button>
</div>
<div class="col-md-4 col-sm-4 col-lg-4 col-xl-4 col-lg-4 left">
<button mat-button color="accent" (click)="prev()" *ngIf="prevBtn">قبلی</button>
</div>
</div>
</mat-select>
点击“完成”按钮后,我希望下拉菜单关闭。如何实现这一点?
答案#1
MatSelect 有 open 和 close 方法可以使用。你可以应用几种方法。
<mat-select #matSelect [formControl]="toppings" multiple>
....
<button (click)="finish(matSelect)">Close</button>
</mat-select>
或者如果你不想将 matSelect 直接传递给 finish() 方法,你可以将它引用到 ViewChild
@ViewChild('matSelect') matSelect;
finish() {
this.matSelect.close();
}
你也可以直接从 HTML 关闭它:
<mat-select #matSelect [formControl]="toppings" multiple>
....
<button (click)="matSelect.close()">Close</button>
</mat-select>
Stackblitz 演示
欲了解更多详情,请查看 API 文档 - Select | Angular Material
