Angular 2+ and Observables: Can't bind to 'ngModel' since it isn't a known property of 'select'

柔情痞子 提交于 2019-11-26 11:25:05

>= RC.5

The FormsModule needs to be imported to make ngModel available

@NgModule({
  imports: [BrowserModule /* or CommonModule */, 
  FormsModule, ReactiveFormsModule],
  ...
)}
class SomeModule {}

<= RC.4

In config.js add

'@angular/forms': {
  main: 'bundles/forms.umd.js',
  defaultExtension: 'js'
},

in main.ts add

import {provideForms, disableDeprecatedForms} from '@angular/forms';

bootstrap(App, [disableDeprecatedForms(),provideForms()])

Plunker example

See also

In app.modules.ts after

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

put:

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

And then in

imports: [
 BrowserModule
 ],

insert the FormsModule something like:

imports: [
 BrowserModule,
 FormsModule
 ],

This was happening to me in my testing suite, despite the fact that I'd already imported FormsModule in my component's *.module.ts file.

In my *.component.spec.ts file, I needed to add both FormsModule and ReactiveFormsModule to the imports list in configureTestingModule:

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

...
TestBed.configureTestingModule({
    imports: [ 
        FormsModule, 
        ReactiveFormsModule,
        ....],
    providers: [MyComponent, ...],
    declarations: [MyComponent],
    schemas: [CUSTOM_ELEMENTS_SCHEMA]
})

This fixed my case.

You need to add the following to your app.module.ts file.

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

And,

@NgModule({
   imports: [
      FormsModule,
      ...
   ]})
rashfmnb

do the following you have to use [ngValue] instead of [val]

<select [(ngModel)]="selectValue">
    <option *ngFor="let entry of entries | async" 
        [ngValue]="entry.value">{{entry.label}}
    </option>
</select>

The above error is caused because you haven't imported FormsModule,ngModel is present in the FormsModule package so,to get rid of this error add import {FormsModule} from '@angular/forms' and add FormsModule in imports in app.module.ts class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!