Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Testing 角度2测试:Can';t绑定到';ngModel';因为它不是';t'的已知属性;输入';_Testing_Angular_Angular Cli_Angular2 Testing - Fatal编程技术网

Testing 角度2测试:Can';t绑定到';ngModel';因为它不是';t'的已知属性;输入';

Testing 角度2测试:Can';t绑定到';ngModel';因为它不是';t'的已知属性;输入';,testing,angular,angular-cli,angular2-testing,Testing,Angular,Angular Cli,Angular2 Testing,我正在尝试测试控件输入的angular2双向绑定。以下是错误: Can't bind to 'ngModel' since it isn't a known property of 'input'. app.component.html <input id="name" type="text" [(ngModel)]="name" /> <div id="divName">{{name}}</div> app.component.spec.ts impor

我正在尝试测试控件
输入的angular2双向绑定。以下是错误:

Can't bind to 'ngModel' since it isn't a known property of 'input'.
app.component.html

<input id="name" type="text" [(ngModel)]="name" />
<div id="divName">{{name}}</div>
app.component.spec.ts

import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { AppService } from './app.service';
describe('App: Cli', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
      providers:[AppService]
    });
  });

  it('divName', async(() => {
    let fixture = TestBed.createComponent(AppComponent);
    let comp = fixture.componentInstance;
    comp.name = 'test';
    fixture.detectChanges();

    let compiled = fixture.debugElement.nativeElement;    
    expect(compiled.querySelector('divName').textContent).toContain('test');
  }));  
});

您需要将
表单模块
导入
测试床
配置

import { FormsModule } from '@angular/forms';

TestBed.configureTestingModule({
  imports: [ FormsModule ],
  declarations: [
    AppComponent
  ],
  providers:[AppService]
});

使用
测试床
可以从头开始为测试环境配置NgModule。这允许您只添加测试所需的内容,而没有可能影响测试的不必要的外部变量。

我也有同样的问题,即使在导入表单模块后,这一问题也没有得到解决。所以我不得不使用ngModel的替代品作为文本字段。请检查以下内容:

总之,我使用[value]像这样为文本字段绑定模型

([value])="searchTextValue"
此外,如果您使用的是日期字段,则需要在ts中绑定模型。在html中,调用该方法

 (dateSelect)="onDateSelect($event)"
在类型脚本中,使用以下代码。这仅适用于使用Ngbdate选择器的情况

  onDateSelect(event) {
  let year = event.year;
  let month = event.month <= 9 ? '0' + event.month : event.month;;
  let day = event.day <= 9 ? '0' + event.day : event.day;;
  let finalDate = year + "-" + month + "-" + day;
  this.finalDateVlaue = finalDate;
 }
onDateSelect(事件){
让年份=event.year;

let month=event.month这个角度的东西看起来很随机。谢谢你的帮助。同意,@PeteB.Dependency injection很好…它可以自动为你做所有事情…只是别忘了在这里导入,没有错误模式和yada yda…这消除了我的错误,但它挂起在Karma中,不会继续创建其他组件之后。现在它被卡住了,没有错误。这为我节省了大量的时间。在角度测试台上的所有这些怪癖。让我发疯。
  onDateSelect(event) {
  let year = event.year;
  let month = event.month <= 9 ? '0' + event.month : event.month;;
  let day = event.day <= 9 ? '0' + event.day : event.day;;
  let finalDate = year + "-" + month + "-" + day;
  this.finalDateVlaue = finalDate;
 }