1

我已经使用 Angular 6 编写了一个应用程序,并且正在向其中添加 Angular 材料。

在现有应用程序中使用反应式表单方法

我有一个具有以下声明的登录模块。

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild([
     {path:'login',component:LoginComponent}
    ]),   
    ReactiveFormsModule,
    HttpClientModule,
    MatFormFieldModule,
    MatInputModule
  ],
  declarations: [LoginComponent]
})
export class LoginModule { }

登录组件具有以下模板 -login.component.html

    <form>
            <mat-form-field>
               <input matInput placeholder="UserName">
            </mat-form-field>
    </form>

AppModule导入其中LoginModule

login.html渲染时出现控制台错误

compiler.js:1016 Uncaught Error: Template parse errors:
No provider for ControlContainer ("<section>

        [ERROR ->]<form>
                <mat-form-field>
                   <input matInput placeholder="UserName">

当我添加如下时FormsModule,​​此错误已解决LoginModule

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild([
     {path:'login',component:LoginComponent}
    ]),
    FormsModule, //forms module added 
    ReactiveFormsModule,
    HttpClientModule,
    MatFormFieldModule,
    MatInputModule
  ],
  declarations: [LoginComponent]
})
export class LoginModule { }

是否FormsModule需要添加 Angular 材料表格才能工作?

但是如果我们在我的示例应用程序中使用响应式表单呢?

4

0 回答 0