mat-select不正常工作

Fir*_*nks 6 angular-material2 angular

这个mat-select元素很有趣.见下文.

<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>
Run Code Online (Sandbox Code Playgroud)

结果

mat-select元素表现得很奇怪

包装mat-select中的a mat-form-field给出了以下错误:

mat-form-field必须包含MatFormFieldControl.您是否忘记将matInput添加到本机输入或textarea元素?

然而,包括inputmatInput内容显示了inputmat-select在一起,使它看起来怪异.有什么方法吗?

Ste*_*neM 9

消息说您的选择必须位于mat-form-field中,而不是与输入相同的mat-form-field.试试这个:

<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>
Run Code Online (Sandbox Code Playgroud)

在他们如何做的文档中:https://material.angular.io/components/form-field/overview

  • 结果我忘了在我的app.module.ts中导入MatSelectModule:p (2认同)

Fir*_*nks 5

愚蠢的我,我忘记了在我中导入MatSelectModule app.module.ts