失败:模板解析错误:Angular 2

时间:2017-09-28 14:01:46

标签: angular unit-testing typescript testing

运行单元测试角度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]
})

1 个答案:

答案 0 :(得分:2)

您应该将FormsModule导入测试模块

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