Michael Brown
Michael Brown

Reputation: 211

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

Theres a bunch of people who already asked this question but I already followed every answer by importing it in my app.module.ts file and even imported it in app.spec.ts file

heres the html file:

<p>{{ randomWord }}</p>
<input type="text" [(ngModel)]="enteredValue">
<button (click)="onSubmit()" >Submit</button>
<p>{{ answer }}</p>

heres the app.module.ts file:

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


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


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


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


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

Upvotes: 9

Views: 23029

Answers (2)

Arvind Chourasiya
Arvind Chourasiya

Reputation: 17372

Adding below code in app.module.ts file solved my issue

Add forms import

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

then add FormsModule in @NgModule's import section

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    AppRoutingModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [ AppComponent ]
})

Upvotes: 5

Jameel Moideen
Jameel Moideen

Reputation: 7931

Import FormModule in imports instead of declarations

declarations: [
    AppComponent,
    TestComponent,

  ],
  imports: [
    BrowserModule,
   FormsModule
  ],

Sample Demo

https://stackblitz.com/edit/angular-ngmodel-stackovf?file=app/app.component.html

Upvotes: 17

Related Questions