【问题标题】:Failed: Template parse errors: Angular 2失败:模板解析错误:Angular 2
【发布时间】:2018-03-10 07:32:15
【问题描述】:

运行单元测试角度 CLI 后,我收到以下错误。以下错误出现在表单上。

Failed: Template parse errors:
There is no directive with "exportAs" set to "ngForm" ("<form [ERROR ->]#userForm = "ngForm" (ngSubmit)="onSubmit(userForm.value)">
    <div class="form-group">
        <label for="e")

html表单

<form #userForm = "ngForm" (ngSubmit)="onSubmit(userForm.value)">
<div class="form-group">
        <label for="exampleInputCountry">Country</label>
        <input class="form-control" id="country" placeholder="Country" ng name="country" ngModel>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

app.module.ts

@NgModule({
  declarations: [
  AppComponent,
  [...]
  ],
  imports: [
  FormsModule,
  [...] 
  ],
  providers: [],
  bootstrap: [AppComponent]
})

【问题讨论】:

    标签: angular unit-testing typescript testing


    【解决方案1】:

    您应该将FormsModule 导入您的测试模块

    TestBed.configureTestingModule({
      ...,
      imports: [
         FormsModule,
      ],
    })
    

    【讨论】:

      猜你喜欢
      • 2017-10-06
      • 1970-01-01
      • 2016-09-24
      • 2017-06-17
      • 2017-09-29
      • 1970-01-01
      • 2017-10-24
      • 2016-09-27
      • 1970-01-01
      相关资源
      最近更新 更多