Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: Template parse errors: Can't bind to 'ngModel' since it isn't a known property of 'input'

Tags:

angular

I got this message when I used the two-way binding [(ngModel)]

Template parse errors: Can't bind to 'ngModel' since it isn't a known property of 'input'.

I understand that importing the FormsModule is suppose to fix this issue as many people got here. However, I did had the FormsModule imported but it doesn't help, issue still be there

Definitely there is something else get wrong with my code. Can you shed a light. Here is my app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule, Routes } from '@angular/router';

import { ValidationModule } from './validation/validation.module';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app.module.routing';

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

}

here is my app.routing.module.ts

import { NgModule } from '@angular/core'
import { RouterModule, Routes } from '@angular/router'

import { Home1Component } from './home1.component';
import { Home2Component } from './home2.component';

const appRoutes = [
  { path: 'home1', component: Home1Component },
  { path: 'home2', component: Home2Component },
  { path: 'validation', loadChildren: './validation/validation.module#ValidationModule'}
];

@NgModule({
    declarations:[
        Home1Component,
        Home2Component
    ],
    imports: [
        RouterModule.forRoot(appRoutes)
    ],
    exports:[
        RouterModule
    ]
})

export class AppRoutingModule {

}

and here is my html

<h1> home 1 </h1>
<form>
    <input [(ngModel)]="currentHero.name">
    <button type="button" (click)="onOkClicked()">Ok</button>
</form>

I attach my source code here, I'm using angular-cli source

like image 501
khoailang Avatar asked Feb 22 '17 13:02

khoailang


People also ask

Can't bind to ngModel since it isn't a known property of input even after importing FormsModule?

Your ngModel is not working because it's not a part of your NgModule yet. You have to tell the NgModule that you have authority to use ngModel throughout your app, You can do it by adding FormsModule into your app. module. ts -> imports -> [] .

Can't bind to since it isn't a known property?

What does "Can't bind to 'x' since it isn't a known property of 'y'" mean? link. This error often means that you haven't declared the directive "x" or haven't imported the NgModule to which "x" belongs. Perhaps you declared "x" in an application submodule but forgot to export it.

Can't bind to ngModel since it isn't a known property of kendo switch?

There are two steps you need to follow to get rid of this error: import FormsModule in your app module. Pass it as value of imports in @NgModule decorator.

What is FormsModule in Angular?

FormsModule. Exports the required providers and directives for template-driven forms, making them available for import by NgModules that import this module. ReactiveFormsModule. Exports the required infrastructure and directives for reactive forms, making them available for import by NgModules that import this module.


1 Answers

You need to add FormsModule to imports in the module where you're using its directives:

@NgModule({
    declarations:[
        Home1Component,
        Home2Component
    ],
    imports: [
        RouterModule.forRoot(appRoutes),
        FormsModule, // <<<=== missing
    ],
    exports:[
        RouterModule
    ]
})
like image 162
Günter Zöchbauer Avatar answered Oct 31 '22 03:10

Günter Zöchbauer