我有一个选择下拉菜单,我想通过单击按钮打开它,文档中的任何地方都没有提到这一点 - 我尝试使用元素引用并在元素上使用 select.open() 但它没有不工作。有没有其他人遇到过这个问题?
<button ngClass="menu-filter-item-button" type="button" mat-button (click)="select.open()">
<strong class="menu-filter-item-title">{{filter.name}}</strong>
<mat-icon>keyboard_arrow_down</mat-icon>
</button>
<mat-select #select multiple (change)="onSubmit($event)" [compareWith]="compareById" [(ngModel)]="filter.value">
<mat-option *ngFor="let value of filter.default" [value]="value">
{{value}}
</mat-option>
</mat-select>
最佳答案
我遇到了同样的问题,这个肮脏的黑客解决了它而不显示 mat-select
本身:
HTML:
<button
mat-icon-button
matTooltip="Language"
(click)=select.open()>
<mat-icon>language</mat-icon>
<mat-select
#select
[(ngModel)]="setLang"
class="langSelect">
<mat-option (click)="changeLang()" value="en">English</mat-option>
<mat-option (click)="changeLang()" value="de">Deutsch</mat-option>
</mat-select>
</button>
CSS:
::ng-deep .langSelect div.mat-select-arrow-wrapper {
display: none;
}
::ng-deep .langSelect.mat-select {
display: inline;
}
在我的项目中,它看起来比 StackBlitz 上的要好,但无论如何这里是 StackBlitz 上此代码的链接.
如果你想简单地有一个额外的按钮来打开 mat-select
这也适合你(不需要 css):
<button mat-icon-button matTooltip="Language" (click)=select.open()>
<mat-icon>language</mat-icon>
<mat-select #select [(ngModel)]="setLang">
<mat-option (click)="changeLang()" value="en">English</mat-option>
<mat-option (click)="changeLang()" value="de">Deutsch</mat-option>
</mat-select>
</button>
关于Angular 2 Material Select 用按钮打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47933634/