我有一张工作得很好的表格。当我在我的项目上运行“NG测试”时,问题就出现了。我使用的是使用CLI构建项目时提供的原始测试用例。
业力输出:
AppComponent should create the app
Failed: Template parse errors:
There is no directive with "exportAs" set to "ngForm" ("
<div id="theForm">
<h2>Bus Form</h2>
<form [ERROR ->]#f="ngForm" name="theForm" (ngSubmit)="addLog(f)">
<div class="form-group">
<label>"): ng:///DynamicTestModule/AppComponent.html@3:12
There is no directive with "exportAs" set to "ngModel" ("
name="boarded"
[(ngModel)]="log.boarded"
[ERROR ->]#logBoarded="ngModel"
pattern="^[0-9]+$"
required>
Component.html:
<title>BusLog</title>
<div id="theForm">
<h2>Bus Form</h2>
<form #f="ngForm" name="theForm" (ngSubmit)="addLog(f)">
<div class="form-group">
<label>Boarded</label>
<input type="number"
class="form-control"
name="boarded"
[(ngModel)]="log.boarded"
#logBoarded="ngModel"
pattern="^[0-9]+$"
required>
<span class="help-block danger" *ngIf="logBoarded.errors?.required && logBoarded.touched">
The # of boarded is required
</span>
<span class="help-block danger" *ngIf="logBoarded.errors?.pattern && logBoarded.touched">
The # of boaurded can only contain numbers
</span>
</div>
组件
import { Component } from '@angular/core';
import { Log } from './log';
import { LogService } from './log.service';
import { Title } from '@angular/platform-browser';
...
模块TS
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
部件规范TS
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
imports: [
FormsModule
]
}).compileComponents();
}));
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
});
it(`should have as title 'BusLog'`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('BusLog');
});
编辑:很抱歉让这个人离开。当我创造这个的时候,我几乎睁不开眼睛。