Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mat-select Not Working Properly

The mat-select element is acting funny. See below.

Code

<mat-form-field>
    <input matInput placeholder="Name" #NameInput>
</mat-form-field>

<mat-select placeholder="How Many?">
    <mat-option>One</mat-option>
    <mat-option>Two</mat-option>
    <mat-option>Three</mat-option>
    <mat-option>Four</mat-option>
    <mat-option>Five</mat-option>
</mat-select>

Results

mat-select element acting weird

Wrapping the mat-select in a mat-form-field gives me the following error:

mat-form-field must contain a MatFormFieldControl. Did you forget to add matInput to the native input or textarea element?

However, including an input with matInput shows both the input and the mat-select together, making it look weird. Any way around this?

like image 278
FiringBlanks Avatar asked Oct 26 '17 14:10

FiringBlanks


People also ask

How do you set mat selected value?

Angular Material Select is created using <mat-select> which is a form control for selecting a value from a set of options. To add elements to Select option, we need to use <mat-option> element and to bind value with <mat-option> , use value property of it.


2 Answers

The message says that your select must be in a mat-form-field, not in the same mat-form-field as your input. Try this:

<mat-form-field>
    <input matInput placeholder="Name" #NameInput>
</mat-form-field>

<mat-form-field>
    <mat-select placeholder="How Many?">
        <mat-option>One</mat-option>
        <mat-option>Two</mat-option>
        <mat-option>Three</mat-option>
        <mat-option>Four</mat-option>
        <mat-option>Five</mat-option>
    </mat-select>
</mat-form-field>

In the doc that's how they do: https://material.angular.io/components/form-field/overview

like image 110
StephaneM Avatar answered Sep 27 '22 16:09

StephaneM


Silly me, I forgot to import MatSelectModule in my app.module.ts.

like image 21
FiringBlanks Avatar answered Sep 27 '22 16:09

FiringBlanks