Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

EDIT: Updated Plunkr: http://plnkr.co/edit/fQ7P9KPjMxb5NAhccYIq?p=preview

this part works:

<div *ngFor="let entry of entries | async">   Label: {{ entry.label }}<br>   Value: {{ entry.value }} </div> 

but I've problems with the select box, the error message is:

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

The whole Component:

//our root app component import {Component} from '@angular/core'; import {NgFor} from '@angular/common'; import {HTTP_PROVIDERS, Http} from '@angular/http'; import 'rxjs/Rx'; import {Observable} from 'rxjs/Rx';  @Component({   selector: 'my-app',   providers: [HTTP_PROVIDERS],   template: `    <select [(ngModel)]="selectValue" name="selectValue">     <option *ngFor="let entry of entries | async"      [value]="entry.value">{{entry.label}}</option>   </select>      <div *ngFor="let entry of entries | async">       Label: {{ entry.label }}<br>       Value: {{ entry.value }}     </div>   `,   directives: [NgFor] }) export class App {    entries: any = {};   selectValue:any;    constructor(private _http: Http) {     this.entries = this._http.get("./data.json")                             .map(res => res.json());   } } 

and data.json

[   {     "timestamp": 0,     "label": "l1",     "value": 1   },   {     "timestamp": 0,     "label": "l2",     "value": 2   },   {     "timestamp": 0,     "label": "l3",     "value": 3       } ] 
like image 675
Lonely Avatar asked Aug 16 '16 20:08

Lonely


People also ask

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

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.

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

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 sub-module but forgot to export it.

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

What Does This Error Mean? Angular is trying to tell us that it doesn't know about the formGroup directive on the <form> element in your component. This usually happens when the wrong forms module is imported, the right module is imported in the wrong place or the ReactiveFormsModule is just not imported at all.

How do you use Ngmodeloptions?

The ng-model-options directive is used to control the binding of an HTML form element and a variable in the scope. You can specify that the binding should wait for a specific event to occur, or wait a specific number of milliseconds, and more, see the legal values listed in the parameter values below.


1 Answers

>= 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

  • https://angular.io/docs/ts/latest/cookbook/rc4-to-rc5.html
like image 88
Günter Zöchbauer Avatar answered Sep 21 '22 18:09

Günter Zöchbauer