Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I always display the placeholder in mat-select intependently of the current selection

I want the mat-select element to always show the placeholder, even if an option is selected.

My HTML code:

<mat-select [formControlName]="'language'" placeholder="Language">
    <mat-option value="de">Deutsch</mat-option>
    <mat-option value="en">English</mat-option>
</mat-select>

The current behavior is: A soon as a language is selected, the placeholder is made small, moved to the top and the selected language is displayed is displayed big.

What I want to have: When a language is selected, still the placeholder should be displayed in big size, the selected language should not be shown.

How can I do that?

like image 588
Codev Avatar asked Dec 10 '22 08:12

Codev


2 Answers

You can use a combination of floatLabel="never" and mat-select-trigger:

<mat-select [formControlName]="'language'" placeholder="Language" floatlLabel="never">
    <mat-select-trigger>Language</mat-select-trigger>
    <mat-option value="de">Deutsch</mat-option>
    <mat-option value="en">English</mat-option>
</mat-select>
like image 142
Badashi Avatar answered Mar 02 '23 00:03

Badashi


You are not providing a label, and in that case, "If not specified, the placeholder will be used as label." (from documentation).

To disable it, provide a Label, and hide it.

<mat-label hidden></mat-label>
<mat-select [formControlName]="'language'" placeholder="Language">
    <mat-option value="de">Deutsch</mat-option>
    <mat-option value="en">English</mat-option>
</mat-select>
like image 30
ForestG Avatar answered Mar 02 '23 00:03

ForestG