mat-select Not Working Properly

冷暖自知 提交于 2019-12-10 13:22:48

问题


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

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?


回答1:


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




回答2:


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




回答3:


I mistakenly put placeHolder [in camelCase] instead of placeholder and it just ignored this!!!

It was very hard to find! but once I changed to placeholder I was able to see it. Just wanted to share in case you may also have run into..



来源:https://stackoverflow.com/questions/46957415/mat-select-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!