簡體   English   中英

無法綁定到“ ngModel”,因為它不是“ select”的已知屬性

[英]Can't bind to 'ngModel' since it isn't a known property of 'select'

使用Angular 2.4.0(使用Angular CLI v1.0.0-rc.0的當前設置)創建的組件,類和服務,以及默認規范。 ng測試效果很好。

添加了一個帶有[(ngModel)]的輸入,現在當我運行“ ng test”時收到上面的消息

app.module.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 { }

missing.component.ts

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() {
  }
}

missing.component.html

<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>

missing.component.spec.ts(默認由“ ng g組件缺失”創建)

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { AbsenceComponent } from './absence.component';

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ AbsenceComponent ]
    })
    .compileComponents();
  }));

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

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

試試下面,

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();
  }));

在此處了解更多信息。

希望這可以幫助!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM