mat-form-fieldにはMatFormFieldControlが含まれている必要があります質問する

mat-form-fieldにはMatFormFieldControlが含まれている必要があります質問する

当社では、独自のフォーム フィールド コンポーネントを構築しようとしています。マテリアル デザインのコンポーネントを次のようにラップしようとしています。

分野:

<mat-form-field>
    <ng-content></ng-content>
    <mat-hint align="start"><strong>{{hint}}</strong> </mat-hint>
    <mat-hint align="end">{{message.value.length}} / 256</mat-hint>
    <mat-error>This field is required</mat-error>
</mat-form-field>

テキストボックス:

<field hint="hint">
    <input matInput 
    [placeholder]="placeholder" 
    [value]="value"
    (change)="onChange($event)" 
    (keydown)="onKeydown($event)" 
    (keyup)="onKeyup($event)" 
    (keypress)="onKeypress($event)">
</field>

使用法:

<textbox value="test" hint="my hint"></textbox>

結果はおおよそ次のようになります。

<textbox  placeholder="Personnummer/samordningsnummer" value="" ng-reflect-placeholder="Personnummer/samordningsnummer">
  <field>
    <mat-form-field class="mat-input-container mat-form-field">
      <div class="mat-input-wrapper mat-form-field-wrapper">
        <div class="mat-input-flex mat-form-field-flex">
          <div class="mat-input-infix mat-form-field-infix">
            <input _ngcontent-c4="" class="mat-input-element mat-form-field-autofill-control" matinput="" ng-reflect-placeholder="Personnummer/samordningsnummer" ng-reflect-value="" id="mat-input-2" placeholder="Personnummer/samordningsnummer" aria-invalid="false">
            <span class="mat-input-placeholder-wrapper mat-form-field-placeholder-wrapper"></span>
          </div>
        </div>
        <div class="mat-input-underline mat-form-field-underline">
          <span class="mat-input-ripple mat-form-field-ripple"></span>
        </div>
        <div class="mat-input-subscript-wrapper mat-form-field-subscript-wrapper"></div>
      </div>
    </mat-form-field>
  </field>
</textbox>

しかし、コンソールに「mat-form-field には MatFormFieldControl が含まれている必要があります」と表示されます。これは、mat-form-field に matInput-field が直接含まれていないことに関係していると思います。ただし、matInput-field は含まれており、ng-content プロジェクション内にあるだけです。

これが電撃戦です:https://stackblitz.com/edit/angular-xpvwzf

ベストアンサー1

私も非常によく似た問題を抱えていました。MatFormFieldModuleメイン モジュールをインポートしましたが、次のように配列MatInputModuleに追加するのを忘れていました。imports

import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from "@angular/material/form-field";


@NgModule({
    imports: [
        MatFormFieldModule,
        MatInputModule
    ]
})
export class AppModule { }

より詳しい情報ここ

おすすめ記事