简体   繁体   English

angular2 测试:无法绑定到“ngModel”,因为它不是“input”的已知属性

[英]angular2 testing: Can't bind to 'ngModel' since it isn't a known property of 'input'

I am trying to test angular2 two-way binding for control input .我正在尝试测试控制input angular2 双向绑定。 Here is the error:这是错误:

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

The app.component.html app.component.html

<input id="name" type="text" [(ngModel)]="name" />
<div id="divName">{{name}}</div>

The app.component.ts app.component.ts

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'  
})
export class AppComponent implements OnInit {
  name: string;    
}

app.component.spec.ts 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');
  }));  
});

You need to import the FormsModule into the TestBed configfuration.您需要将FormsModule导入到TestBed配置中。

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

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

What you are doing with the TestBed is configuring a NgModule from scratch for the test environment.您使用TestBed是从头开始为测试环境配置 NgModule。 This allows you to only add what is needed for the test without having unnecessary outside variables that may affect the test.这允许您只添加测试所需的内容,而不会添加可能影响测试的不必要的外部变量。

I had the same issue, even after importing forms module this was not solved.我有同样的问题,即使在导入表单模块后也没有解决。 So I had to use alternative to ngModel for text field.所以我不得不在文本字段中使用 ngModel 的替代品。 Please check this link :请检查此链接

In summary i had used [value] to bind the model for the text field like this.总之,我使用 [value] 来绑定文本字段的模型,如下所示。

([value])="searchTextValue"

Also,if you are using date field you need to bind the model in ts.此外,如果您使用日期字段,则需要在 ts 中绑定模型。 in the html, call the method在html中,调用方法

(dateSelect)="onDateSelect($event)"

In the type script, use the following code.This is applicable only if you are using Ngbdate picker.在类型脚本中,使用以下代码。这仅在您使用 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;
}

暂无
暂无

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

相关问题 错误:“NG0303:无法绑定到‘max’,因为它不是‘mat-progress-bar’的已知属性。” 同时 angular 测试 - ERROR: 'NG0303: Can't bind to 'max' since it isn't a known property of 'mat-progress-bar'.' while angular testing 使用 Karma 进行测试-无法绑定到“routerLink”,因为它不是 - Testing with Karma- Can't bind to 'routerLink' since it isn't a known property of Ng 测试 - 无法绑定到“value”,因为它不是“mat-select”的已知属性 - Ng Test - Can't bind to 'value' since it isn't a known property of 'mat-select' 许多失败:模板解析错误:无法绑定到“routerLink”,因为它不是“a”的已知属性。 Karma 中的错误 - Lots of Failed: Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a'. errors in Karma 角度测试:量角器无法获得输入值 - Angular testing: Protractor can't get input value 在Angular2 / Jasmine中测试选择的输入更改 - Testing select input change in Angular2/Jasmine 带有旁观者的 Angular 组件测试记录了模拟子组件的“无法绑定到输入”警告 - Angular component test with spectator logs "can't bind to input" warnings for mocked child component angular 2测试-无法创建组件 - angular 2 testing - Can't create a component Angular2组件测试-错误:无法解析所有参数 - Angular2 Component test - Error: Can't resolve all parameters Angular2 / Jasmine:间谍程序的单元测试方法未看到被调用的方法 - Angular2 / Jasmine: Spy on unit testing methods doesn't see called method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM