我想在我的应用程序中实现Angular Material单选组,出现以下错误:
ERROR Error: mat-form-field must contain a MatFormFieldControl.
at getMatFormFieldMissingControlError
这是我的代码:
组件:
this.taskForm = this._formBuilder.group({
Schedule: ['', Validators.required],
StartDate: ['', Validators.required],
EndDate: ['', Validators.required],
});
component.html:
<div>
<mat-form-field>
<label id="example-radio-group-label">Schedule: </label>
<mat-radio-group aria-labelledby="example-radio-group-label" *ngFor="let season of seasons" formControlName="Schedule" class="example-radio-group">
<label for="{{season}}" class="radio-inline"></label>
<mat-radio-button id="{{season}}" class="example-radio-button" [value]="season" [checked]="season==='Spring'" >
{{season}}
</mat-radio-button>
</mat-radio-group>
</mat-form-field>
</div>
答案 0 :(得分:2)
修复代码中的两件事:
1-将您的代码放在mat-form-field之外,了解更多(https://v5.material.angular.io/components/form-field/overview)
2-更新代码,然后将ngFor放在mat-radio-button内
<div>
<label id="example-radio-group-label">Schedule: </label>
<mat-radio-group aria-labelledby="example-radio-group-label" formControlName="Schedule" class="example-radio-group">
<label for="{{season}}" class="radio-inline"></label>
<mat-radio-button id="{{season}}" *ngFor="let season of seasons" class="example-radio-button" [value]="season" >
{{season}}
</mat-radio-button>
</mat-radio-group>
</div>