angular - 导入 mat-checkbox 时出现角度材料错误
问题描述
我在使用 Angular Material 时遇到了一些问题,我尝试了很多解决方案,但都没有奏效。这就是我想要做的:
我正在使用 Angular Material 和 Reactive Forms 来制作register
表单,在我添加mat-checkbox
组件之前一切都很好。这是error
我得到的:
ERROR 错误:mat-form-field 必须包含 MatFormFieldControl。
这是我的代码:
HTML:
<mat-form-field>
<mat-checkbox formControlName="check">
Check me!
</mat-checkbox>
</mat-form-field>
零件 :
this.registerForm = this.formBuilder.group({
name: ['', Validators.required ],
email: ['', Validators.required],
check: ['', Validators.required ]
});
模块 :
import { ReactiveFormsModule } from '@angular/forms';
import { RegisterFormComponent } from './register-form.component';
import { MatCheckboxModule } from '@angular/material';
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@NgModule({
imports: [
ReactiveFormsModule,
MatInputModule,
MatFormFieldModule,
MatCheckboxModule,
BrowserAnimationsModule
],
declarations: [
RegisterFormComponent
]
})
export class RegisterFormModule { }
我导入了模块,以便 Angular Material 正常工作,实现了表单控件名称,但我仍然得到了相同的error
. 我试图在没有 mat-form-field 容器的情况下将其包含mat-checkbox
在我的html中,并且它可以完美地工作而没有错误,但我确实需要使用表单字段,因为我想添加一些 mat-error 组件来显示验证消息。
有谁知道我错过了什么?
解决方案
该错误意味着表单字段在其中找不到兼容的材料输入。
不要<mat-checkbox>
在里面使用<mat-form-field>
。
以下 Angular Material 组件设计用于在 内部工作
<mat-form-field>
:
<input matNativeControl>
&<textarea matNativeControl>
(第 7 版及更高版本)
<select matNativeControl>
(第 7 版及更高版本)
<input matInput>
&<textarea matInput>
(版本 5 和 6)
<mat-select>
<mat-chip-list>
来源:官方文档
推荐阅读
- angular - 如何在打字稿的Angular2 highcharts中添加鼠标滚轮代码
- php - 如何将 HTML 文件更改为 PHP
- asp.net-core - 如何正确使用 IdentityServer4 的声明?
- security - 安全存储 API 密钥:环境与 JSON
- ruby-on-rails - Rails 5,Postgesql 和按关联分组
- couchdb - 与织物作曲家一起使用沙发数据库时出错
- sql - 使用 SQL 将数据插入 Excel 文件
- android - android - 如何使用 Room 从 db 获取简单列表?
- android - 如何绘制日志图
- windows - 在 Windows 上使用 FFMPEG 进行 IPC 的最佳方法是什么