基于最长选项宽度的角材料垫选择宽度

时间:2019-02-04 14:16:34

标签: css angular sass width angular-material2

我有一个带选项的mat-select选项,我希望将此内联设置为text,并且下拉列表的长度只能与最长的选项一样长。我可能可以使用js / css香草样式来做一些黑客,但是正在寻找更好的解决方案。有什么想法吗?

<mat-select
  [ngClass]="{'missing-selection': !SelectedOption}"
    [(value)]="SelectedOption"
    id="select"
    (selectionChange)="optionChange($event)"
  >
    <mat-option
      *ngFor="let option of data.Options"
      [value]="option.Value"
      >{{ option.Label}}</mat-option
    >
  </mat-select>

2 个答案:

答案 0 :(得分:1)

请尝试此操作(针对所选选项)...

.auto-width{
    .mat-form-field {
        width: auto !important;
    }
    .mat-select-value {
        max-width: 100%;
        width: auto;
    }  
}

<div class="auto-width">
    <mat-form-field>
      <mat-select>
        <mat-option value="long">Long description</mat-option>
        <mat-option value="foo">Foo</mat-option>      
      </mat-select>
    </mat-form-field>
</div>

答案 1 :(得分:0)

尝试此代码,它可以工作,但不是动态的。

::ng-deep .mat-form-field {
  width: 50px !important;
}
::ng-deep .mat-label {
    width: 50px !important;
}
::ng-deep .mat-select {
    width: 50px !important;
}

::ng-deep .mat-option {
    width: 50px !important;
}
相关问题