将样式添加到 mat-autocomplete 的 mat-option

Use*_*614 3 html css sass angular-material angular

我这里有这个 HTML代码

<div style="width: 150px">
  <form class="example-form">
  <mat-form-field class="example-full-width">
    <input type="text" placeholder="Assignee" aria-label="Assignee" matInput [formControl]="myControl" [matAutocomplete]="auto">
    <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
      <mat-option *ngFor="let option of filteredOptions | async" [value]="option">
        {{option.name}}
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
</form>
</div>
Run Code Online (Sandbox Code Playgroud)

的默认行为mat-option是使用


  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis

Run Code Online (Sandbox Code Playgroud)
  1. 我想将长句子分成下一行而不是使用.... 样式不起作用:(

  2. 我还需要覆盖我尝试使用下面的代码的字体,但它不会覆盖

mat-option {
    font-family: "Times New Roman", Times, serif; // does not work
  }
Run Code Online (Sandbox Code Playgroud)

在这里设计

请帮忙。

Vin*_*Lim 8

您可以将其添加到 CSS 中:

.mat-option{
  word-wrap:break-word !important;
  white-space:normal !important;
  font-family: "Times New Roman", Times, serif;
}

.mat-option-text{
  line-height:initial !important;
}
Run Code Online (Sandbox Code Playgroud)


kve*_*tis 5

您的 stlying 不起作用的原因是因为实际选项显示在放置在<body>. 因此选择器component mat-option无法工作,因为选项没有放置在组件中。

要覆盖中的任何样式,mat-option您最好解决该类.mat-option并将其放入全局中styles.scss

.mat-option {
   font-family: "Times New Roman", Times, serif; // 
}
Run Code Online (Sandbox Code Playgroud)