No provider for ControlContainer Error while using Angular Material Forms with Angular 6

十年热恋 提交于 2021-01-29 03:57:19

问题


I have already written an application using Angular 6 and I am adding Angular material into it.

Reactive forms approach is used in existing application

I have a login module which has below declaration.

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

The Login component has below template - login.component.html

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

The AppModule imports the LoginModule into it.

I am getting below console error when login.html is rendered

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

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

This error is resolved when I add FormsModule in LoginModule as below

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

Does FormsModule needed to be added for Angular material forms to work ?

But what if we are using reactive forms as the case with my sample application ?

来源:https://stackoverflow.com/questions/52623498/no-provider-for-controlcontainer-error-while-using-angular-material-forms-with-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!