【发布时间】:2021-03-22 11:21:37
【问题描述】:
我正在尝试在页面上实现表单组,但是发生了这个错误:
ERROR Error: No value accessor for form control with name: 'modalidade'
at _throwError (forms.js:2431)
at setUpControl (forms.js:2339)
at FormGroupDirective.addControl (forms.js:5475)
at FormControlName._setUpControl (forms.js:6057)
at FormControlName.ngOnChanges (forms.js:5988)
at FormControlName.rememberChangeHistoryAndInvokeOnChangesHook (core.js:2131)
at callHook (core.js:3042)
at callHooks (core.js:3008)
at executeInitAndCheckHooks (core.js:2960)
at selectIndexInternal (core.js:6180)
ERROR Error: No value accessor for form control with name: 'unidade'
at _throwError (forms.js:2431)
at setUpControl (forms.js:2339)
at FormGroupDirective.addControl (forms.js:5475)
at FormControlName._setUpControl (forms.js:6057)
at FormControlName.ngOnChanges (forms.js:5988)
at FormControlName.rememberChangeHistoryAndInvokeOnChangesHook (core.js:2131)
at callHook (core.js:3042)
at callHooks (core.js:3008)
at executeInitAndCheckHooks (core.js:2960)
at selectIndexInternal (core.js:6180)
ERROR Error: No value accessor for form control with name: 'curso'
at _throwError (forms.js:2431)
at setUpControl (forms.js:2339)
at FormGroupDirective.addControl (forms.js:5475)
at FormControlName._setUpControl (forms.js:6057)
at FormControlName.ngOnChanges (forms.js:5988)
at FormControlName.rememberChangeHistoryAndInvokeOnChangesHook (core.js:2131)
at callHook (core.js:3042)
at callHooks (core.js:3008)
at executeInitAndCheckHooks (core.js:2960)
at selectIndexInternal (core.js:6180)
ERROR Error: mat-form-field must contain a MatFormFieldControl.
at getMatFormFieldMissingControlError (form-field.js:227)
at MatFormField._validateControlChild (form-field.js:707)
at MatFormField.ngAfterContentInit (form-field.js:514)
at callHook (core.js:3038)
at callHooks (core.js:3008)
at executeInitAndCheckHooks (core.js:2960)
at refreshView (core.js:7213)
at refreshEmbeddedViews (core.js:8280)
at refreshView (core.js:7196)
at refreshComponent (core.js:8326)
我的 app.module.ts 正在导入 MatFormFieldModule:
imports: [
CommonModule,
BrowserModule,
AppRoutingModule,
FormsModule,
BrowserAnimationsModule,
MatIconModule,
MatRadioModule,
MatTableModule,
MatInputModule,
MatFormFieldModule,
MatPaginatorModule,
MatTabsModule,
MatProgressSpinnerModule,
ReactiveFormsModule,
MatSlideToggleModule,
StoreModule.forRoot(reducers),
EffectsModule.forRoot([AuthEffects]),
MatSortModule
]
这是我来自组件的 html 和 ts 代码:
HTML
<form [formGroup]="filtroForm">
<mat-form-field appearance="none">
<mat-select formControlName="modalidade">
<mat-select-trigger>
Modalidade
</mat-select-trigger>
<mat-option value=""></mat-option>
<mat-option *ngFor="let modalidade of filtroOptions.modalidade" [value]="modalidade">{{modalidade}}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field appearance="none">
<mat-select formControlName="unidade">
<mat-select-trigger>
Unidade
</mat-select-trigger>
<mat-option value=""></mat-option>
<mat-option *ngFor="let unidade of filtroOptions.unidade" [value]="unidade">{{unidade}}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field appearance="none">
<mat-select formControlName="curso">
<mat-select-trigger>
Curso
</mat-select-trigger>
<mat-option value=""></mat-option>
<mat-option *ngFor="let curso of filtroOptions.curso" [value]="curso">{{matriz}}</mat-option>
</mat-select>
</mat-form-field>
</form>
TS
filtro = {
modalidade: "Todas",
unidade: "Todas",
curso: "Todas"
};
filtroOptions = {
modalidade: [],
unidade: [],
curso: []
}
filtroForm = new FormGroup({
modalidade: new FormControl("Todas"),
unidade: new FormControl("Todas"),
curso: new FormControl("Todas")
});
我不知道为什么会发生这个错误,我在另一个 Angular 9 项目中有相同的表单字段并且它正在工作。我已经在这上面浪费了几个小时,我尝试过以其他方式实现 FormGroup,例如使用 FormBuilder,但没有成功。
【问题讨论】:
-
最好将您的代码放在stackblitz.com 上,这样可以更轻松地查看和解决问题。
标签: html angular typescript