Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngModel not working in Angular4

I am learning Angular 4 from the official site and I came to the part with 2-way data binding through ngModel. However, my app stops working as soon as I add [(ngModel)] to my component template, even though the FormsModule is imported in the module.ts file. The component does not load.
I am using Visual Studio Code.
This is my app.component.ts

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

    export class Hero {
      id: number;
      name: string;
    }



    @Component({
      selector: 'app',
      template: `
      <h1>{{ title }}</h1>
      <h2>{{hero.name}} details!</h2>
      <div><label>Id:</label> {{hero.id}} </div>
      <div>name:<input [(ngModel)]="hero.name" type="text"></div>
      `,
      styles:[`
        .selected{
          transition: padding 0.3s;
          color: mediumseagreen;
        }
      `]
    })

    export class AppComponent {
      title = 'Tour Of Heroes';

     hero:Hero = {
       id:1,
       name:"Mr. Invisible"
     };
    }  

This is app.module.ts

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

    import { AppComponent, Hero } from './app.component';

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

The AppComponent is not loaded and just shows

Loading...

like image 465
Mavil Avatar asked Jun 28 '17 05:06

Mavil


People also ask

Can't bind to ngModel since it isn't a known property of P chips?

To fix Can't bind to 'ngModel' since it isn't a known property of 'input' error in Angular applications we have to import FormModule in app. module. ts file. If you are using FormBuilder class to create reactive form we have to import ReactiveFormsModule as well to avoid below error.

Where can I import ngModel?

If the user changes the value inside the input field, the Angular property will also change its value. The ngmodel directive is not part of the Angular Core library. It is part of the FormsModule library. You need to import the FormsModule package into your Angular module.

What is the difference between ngModel and [( ngModel )]?

The answer is: (ngModel) causes a 1-way data-binding, whereas [(ngModel)] ensures a two-way data binding.


2 Answers

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

    import { AppComponent, Hero } from './app.component';

    @NgModule({
      declarations: [
        AppComponent

      ],
      imports: [
        BrowserModule,
        FormsModule  // forms module should be in imports not in declarations
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { } 
like image 63
Rahul Singh Avatar answered Oct 08 '22 19:10

Rahul Singh


As others pointed out, it is important to import the FormsModule (e.g. in your app.module.ts file.) In this specific question, it was missing in the import section.

But I added this answer to warn you about something else, which I run into from time to time.

If you just use ngModel standalone, without a form somewhere, you don't have to specify a name for the input.

<input ... [(ngModel)]="model.something"`>

But when you do use it in a form, the name attribute becomes mandatory !

<form>
  <input ... name="something" [(ngModel)]="model.something"`>
</form>

It's actually in the docs:

When using the ngModel within tags, you'll also need to supply a name attribute so that the control can be registered with the parent form under that name.

If you miss it, it won't show any errors at all, it just won't work.

like image 44
bvdb Avatar answered Oct 08 '22 17:10

bvdb