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

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


You need to import the FormsModule into the TestBed configfuration.

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. 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. Please check this link:

In summary i had used [value] to bind the model for the text field like this.

([value])="searchTextValue"

Also,if you are using date field you need to bind the model in ts. in the html, call the method

(dateSelect)="onDateSelect($event)"

In the type script, use the following code.This is applicable only if you are using Ngbdate picker.

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