1

I have imported FormsModule in my app.module.ts but I am still getting this error.

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

I have gone through similar posts but still I am unable to find a solution for this.

app.module.ts

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

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

app.component.ts

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

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'


})
export class AppComponent {
}

app.component.html

<input type=radio name="gender" value="Mr" [(ngModel)]="gender">Male
<input type=radio name="gender" value="Mrs" [(ngModel)]="gender">Female
{{"Hello " + gender}}
2
  • 2
    why do you have a ngModule in appcomponent? Commented Oct 23, 2017 at 11:50
  • 2
    you should really learn how an angular apps work. The best way to start is to understand this example: angular.io/tutorial Commented Oct 23, 2017 at 11:53

1 Answer 1

5

Try like this :

app.module.ts

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

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

app.component.ts

import { Component } from '@angular/core';

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

app.component.html

<input type=radio name="gender" value="Mr" [(ngModel)]="gender">Male
<input type=radio name="gender" value="Mrs" [(ngModel)]="gender">Female
{{"Hello " + gender}}
Sign up to request clarification or add additional context in comments.

1 Comment

It worked. Thanks a lot :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.