在Angular4中,每当我添加表单标记时,获取ControlContainer的错误提供程序错误

时间:2018-03-31 05:11:32

标签: forms angular-cli angular-reactive-forms

首先,我在app.module.ts

中添加了ReactiveFormModule
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes} from '@angular/router';
import { ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { NewproducComponent } from './comp/newproduc/newproduc.component';
import { ProductsComponent } from './comp/products/products.component';
import { SidebarComponent } from './comp/sidebar/sidebar.component';
import { MenubarComponent } from './comp/menubar/menubar.component';


const appRoutes: Routes = [
  { path : '' , component : ProductsComponent},
  { path : 'newproduct' , component : NewproducComponent}
];


@NgModule({
  declarations: [
    AppComponent,
    NewproducComponent,
    ProductsComponent,
    SidebarComponent,
    MenubarComponent
  ],
  imports: [
    BrowserModule,
    ReactiveFormsModule,
    RouterModule.forRoot(appRoutes) 
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {
 }

app.component.html contanis

<app-menubar></app-menubar>
<app-sidebar></app-sidebar>
<div class="container">
  <router-outlet></router-outlet>
</div>

newproduc.component.html包含

<div id="main-body">
  <div class="main-body-header">
            <span>New Product</span>
            <button type="button">Close</button>
            <button type="button">Save</button>
  </div>

  <div class="main-body-component">
    <div class="container">
       <form>

        </form>
    </div>

  </div>

</div>

添加此表单标记时出现以下错误

Error in browser console

每当我删除错误删除的表单标记时。我不确定我在哪里犯了这个错误。请帮我解决这个问题。我已经堆积了最近2天。

1 个答案:

答案 0 :(得分:1)

执行:

<form [formGroup]="form">

由于您要导入ReactiveFormsModule,因此<form>代码必须包含formGroup