首页 文章

如何自定义mat-select下拉位置?

提问于
浏览
5

自从我没有找到解决方案以来,已经有几天了 . 由于mat-menu的选项是overlaptrigger属性,但在mat select下拉列表中没有可执行此操作的属性 . 有没有办法通过覆盖css或任何更好的解决方案来自定义mat-select下拉位置 .

2 回答

  • 7
    <mat-select placeholder="Language" disableOptionCentering panelClass="myPanelClass">
                        <mat-option *ngFor="let locale of locales" [value]="locale">
                            {{locale}}
                        </mat-option>
                    </mat-select>
    

    像上面一样使用 disableOptionCenteringpanelClass . 然后在您的 styles.scss 引用你的panelClass并做

    .myPanelClass{
            margin-top: 30px !important; 
        }
    

    这对我有用 . 注意scss must be in your styles.scss not your component's scss file . 你可能不需要在这里使用重要的,我有其他类围绕这个,所以我确实使用它 .

  • 1

    我发现解决方案为 disableOptionCentering direcrive为 mat-select ,因此使用此dropdaown后可以自定义 .

    来自timmoy:https://github.com/angular/material2/pull/9885

    用法示例:

    <mat-select
        [(ngModel)]="currentPokemon"
        [required]="pokemonRequired" 
        [disabled]="pokemonDisabled" 
        #pokemonControl="ngModel" 
        disableOptionCentering>
    <mat-option 
        *ngFor="let creature of pokemon" 
        [value]="creature.value">
    {{ creature.viewValue }}
    </mat-option>
    </mat-select>
    

相关问题