8

根据文档中的示例,我正在使用带有输入的 mat-autocomplete 组件,并且我已将输入配置为使用标签并具有非浮动占位符文本,如下所示:

<mat-form-field floatLabel="never">
  <input 
    type="text" 
    placeholder="Search..." 
    aria-label="Number" 
    matInput 
    [formControl]="search" 
    [matAutocomplete]="auto">
  <button 
    mat-button 
    *ngIf="search.value" 
    matSuffix 
    mat-icon-button 
    aria-label="Clear" (click)="clearSearch()">
      <mat-icon>close</mat-icon>
  </button>
  <mat-autocomplete 
    #auto="matAutocomplete" 
    (optionSelected)="goToResult($event)">
    <mat-option 
      *ngFor="let result of searchResults" 
      [value]="result">
      {{result.id}}
    </mat-option>
  </mat-autocomplete>
</mat-form-field>

单击输入开始输入字符时,占位符不会消失,直到我输入第一个字符。我错过了一些应该设置的配置\属性吗?

还是我需要设置一个占位符值绑定并自己设置\清除它?

谢谢

4

3 回答 3

16

您可以删除输入中的占位符并在其中添加 amat-placeholdermat-form-field使用类自定义 css。

HTML 文件:

<form class="example-form">
  <mat-form-field floatLabel="never">
      <input 
        matInput 
        type="text" 
        aria-label="Number" 
        matInput [formControl]="myControl" 
        [matAutocomplete]  ="auto">

      <mat-placeholder class="placeholder">Search</mat-placeholder>

      <mat-autocomplete #auto="matAutocomplete">
        <mat-option *ngFor="let option of options" [value]="option">
          {{option}}
        </mat-option>
      </mat-autocomplete>
  </mat-form-field>
</form>

CSS 文件:

.mat-focused .placeholder {
    color: transparent;
}

.example-form {
  min-width: 150px;
  max-width: 500px;
  width: 100%;
}

.example-full-width {
  width: 100%;
}
于 2018-08-27T07:04:16.487 回答
2

仅使用 OOTBmatInput并隐藏占位符“浮动”

/*hide the floating mat input part */
.mat-form-field-should-float .mat-form-field-label-wrapper {display: none;}

完整的解决方案

.middle-cut .mat-form-field-should-float .mat-form-field-label-wrapper {display: none;}
.middle-cut .mat-form-field-should-float .mat-form-field-infix {    margin-top: -10px;}
.middle-cut .mat-form-field-infix  {border:0; padding-top:0; padding: .1375em 0; }
.middle-cut .mat-form-field-label{top: 0.85em;}
于 2019-03-17T02:16:53.030 回答
0

您可以使用 mat-placeholder 并通过在 (focus) 事件上添加一个类来隐藏它,这将隐藏占位符。请参阅此链接https://stackoverflow.com/a/60021883/9026103

于 2020-02-01T23:12:45.470 回答