有人可以告訴我如何將 mat-select 欄位文本固定在組件的邊框上,就像這樣(如果可能的話,也可以放置占位符),即使沒有點擊它:

默認情況下,文本位于組件中間,并且在單擊事件時,文本到他的邊框:

我的組件實作:
<mat-form-field appearance="outline">
<mat-label>Age</mat-label>
<mat-select matNativeControl required>
<mat-option *ngFor="let age of ages" [value]="age">{{age}}</mat-option>
</mat-select>
</mat-form-field>
我試圖在開發人員工具上檢查負責在單擊時格式化組件的 css,但沒有成功。
uj5u.com熱心網友回復:
您需要使用floatLabel輸入。
檢查檔案
<mat-form-field appearance="outline" floatLabel="always">
<mat-label>Age</mat-label>
<mat-select matNativeControl required>
<mat-option *ngFor="let age of ages" [value]="age">{{age}}</mat-option>
</mat-select>
</mat-form-field>
如果您希望在所有表單欄位中都有這種行為,您可以使用MAT_FORM_FIELD_DEFAULT_OPTIONS提供默認配置。
import {MAT_FORM_FIELD_DEFAULT_OPTIONS} from '@angular/material/form-field';
...
providers: [
{
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS,
useValue: { floatLabel: 'always' }
}
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/529112.html
下一篇:ngFor將字串字面量型別轉換為字串,拋出“元素隱式具有'any'型別,因為不能使用'string'型別的運算式......”
