无法绑定到'ngModel',因为它不是'select'的已知属性

时间:2017-03-07 19:01:58

标签: angular karma-jasmine

使用Angular 2.4.0(使用Angular CLI v1.0.0-rc.0进行当前设置) 创建的组件,类和服务,创建默认规范。 ng测试工作正常。

在其中添加了带[[ngModel]]的输入,现在我在运行“ng test”时收到上面的消息

app.module.ts

CREATE TABLE Persons
(
 P_Id int NOT NULL,
 LastName varchar(255) NOT NULL,
 FirstName varchar(255),
 Address varchar(255),
 City varchar(255),
 CHECK_GEN_FIELD int AS (CASE WHEN P_Id>0 THEN 1 ELSE NULL END) NOT NULL
);

absence.component.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent,
    AbsenceComponent,
  ],
  imports: [
    BrowserModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

absence.component.html

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-absence',
  templateUrl: './absence.component.html',
  styleUrls: ['./absence.component.css']
})
export class AbsenceComponent implements OnInit {
  selectedValue = null;
  constructor() { }

  ngOnInit() {
  }
}

absence.component.spec.ts(默认由“ng g component absence”创建)

<select [(ngModel)]="selectedValue">
  <option value="0">Value 0</option>
  <option value="1">Value 1</option>
  <option value="2">Value 2</option>
  <option value="3">Value 3</option>
</select>

1 个答案:

答案 0 :(得分:5)

尝试以下,

beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ FormsModule ],  
     // you need to import, provide anything you need in the component 
     // so that they can be used\injected in the component for test.
      declarations: [ AbsenceComponent ]
    })
    .compileComponents();
  }));

详细了解here

希望这会有所帮助!!