简体   繁体   English

无法绑定到“ formGroup”,因为它不是“ form”的已知属性。 (“角7

[英]Can't bind to 'formGroup' since it isn't a known property of 'form'. (" in angular 7

Running angular 7 'ng test', it gives me the error: 运行angular 7'ng test',它给我错误:

Failed: Template parse errors:
Can't bind to 'formGroup' since it isn't a known property of 'form'. ("

Everything I've seen posted is basically 'add FormsModule and ReactiveFormsModule to app.module' or to whatever module is using the component. 我看到的所有内容基本上都是“将FormsModule和ReactiveFormsModule添加到app.module”或正在使用该组件的任何模块。 I've only got the one module, and it is importing them. 我只有一个模块,它正在导入它们。 Karma is not relenting though, and is killing me with this error. 业力并没有放松,而是因为这个错误杀死了我。

c-runner.component: c-runner.component:

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators, ReactiveFormsModule } from '@angular/forms';

@Component({
  selector: 'app-c-runner',
  templateUrl: './c-runner.component.html',
  styleUrls: ['./c-runner.component.scss']
})
export class CRunnerComponent implements OnInit {

  cForm = new FormGroup({
    a_id: new FormControl('', Validators.required),
    u_id: new FormControl('', Validators.required)
  });

  ...
}

app.module: app.module:

...
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
...
import { CRunnerComponent } from './c-runner/c-runner.component';

@NgModule({
  declarations: [
    AppComponent,
    CRunnerComponent
  ],
  imports: [
    ...
    FormsModule,
    ReactiveFormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

c-runner.html c-runner.html

<div class="container">
  <div class="d-flex justify-content-center h-100">
<div class="card bg-dark">
  <div class="card-header">
    <h3>Run</h3>
  </div>
  <div class="card-body">
    <form (ngSubmit)="runC()" [formGroup]="cForm">
      <div class="input-group form-group">
        <div class="input-group-prepend">
          <span class="input-group-text">File</span>
        </div>
        <select id="a_file" class="form-control" required formControlName="a_id">
          <option value="" disabled selected>Select File</option>
          <option *ngFor="let tf of a_files" [(value)]="tf.id">{{ tf.filename }}</option>
        </select>
      </div>
      <div class="input-group form-group">
        <div class="input-group-prepend">
          <span class="input-group-text">File</span>
        </div>
        <select id="u_file" name="u_file" class="form-control" required formControlName="u_id">
          <option value="" disabled selected>Select File</option>
          <option *ngFor="let tf of u_files" [(value)]="tf.id">{{ tf.filename }}</option>
        </select>
      </div>
      <div class="form-group">
        <input type="submit" value="Run" class="btn btn-secondary float-right" [disabled]="!cForm.valid">
      </div>
    </form>
  </div>
</div>

cgTag was on the right track. cgTag处于正确的轨道。 Test bed changed to include a bunch of imports as shown below. 测试台更改为包括一堆进口,如下所示。 Hope this helps someone in the future! 希望这对以后的人有所帮助!

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CRunnerComponent } from './c-runner.component';
import { Component } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { RouterTestingModule } from '@angular/router/testing';

describe('CRunnerComponent', () => {
  let component: CRunnerComponent;
  let fixture: ComponentFixture<CRunnerComponent>;

  @Component({
    selector: 'app-upload-modal',
    template: '',
    inputs: ['filenames', 'upload_type']
  })
  class MockUploadModalComponent {}

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        CRunnerComponent,
        MockUploadModalComponent
      ],
      imports: [
        FormsModule,
        ReactiveFormsModule,
        HttpClientModule,
        RouterTestingModule
      ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(CRunnerComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 6:无法绑定到“formGroup”,因为它不是“form”的已知属性? - Angular 6: Can't bind to 'formGroup' since it isn't a known property of 'form'? Angular 4无法绑定到&#39;formGroup&#39;,因为它不是&#39;form&#39;的已知属性 - Angular 4 Can't bind to 'formGroup' since it isn't a known property of 'form' Angular 9 - 无法绑定到“formGroup”,因为它不是“form”的已知属性 - Angular 9 - Can't bind to 'formGroup' since it isn't a known property of 'form' Angular 9 - 无法绑定到“formGroup”,因为它不是“form”的已知属性,即使正在导入 FormsModule 和 FormBuilder - Angular 9 - Can't bind to 'formGroup' since it isn't a known property of 'form' even though FormsModule and FormBuilder are being imported 无法绑定到“formGroup”,因为它不是登录组件中“form”的已知属性 - Can't bind to 'formGroup' since it isn't a known property of 'form' just in logincomponent Angular 7:无法绑定到“元素”,因为它不是 - Angular 7 : Can't bind to 'element' since it isn't a known property of 无法绑定到“”,因为它不是“ angular 2”的已知属性 - can't bind to '' since it isn't a known property of '' angular 2 带有 Angular 的 NG2 图表:无法绑定到“颜色”,因为它不是 Angular 中“画布”的已知属性 13 - NG2 Chart with Angular: Can't bind to 'colors' since it isn't a known property of 'canvas' in Angular 13 无法绑定到“控件”,因为它不是(myComponent)的已知属性 - Can't bind to 'control' since it isn't a known property of (myComponent) 无法绑定到“ useStickyClasses”,因为它不是“ div”的已知属性 - Can't bind to 'useStickyClasses' since it isn't a known property of 'div'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM