Angular 2+和Observables:无法绑定到'ngModel',因为它不是'select'的已知属性

Lon*_*ely 19 json asynchronous drop-down-menu angular2-observables angular

编辑:更新了Plunkr:http://plnkr.co/edit/fQ7P9KPjMxb5NAhccYIq?p = preview

这部分有效:

<div *ngFor="let entry of entries | async">
  Label: {{ entry.label }}<br>
  Value: {{ entry.value }}
</div>
Run Code Online (Sandbox Code Playgroud)

但我对选择框有问题,错误信息是:

无法绑定到'ngModel',因为它不是'select'的已知属性

整个组件:

//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());
  }
}
Run Code Online (Sandbox Code Playgroud)

和data.json

[
  {
    "timestamp": 0,
    "label": "l1",
    "value": 1
  },
  {
    "timestamp": 0,
    "label": "l2",
    "value": 2
  },
  {
    "timestamp": 0,
    "label": "l3",
    "value": 3    
  }
]
Run Code Online (Sandbox Code Playgroud)

Gün*_*uer 19

> = RC.5

FormsModule要导入的需求做出ngModel可用

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

<= RC.4

config.js

'@angular/forms': {
  main: 'bundles/forms.umd.js',
  defaultExtension: 'js'
},
Run Code Online (Sandbox Code Playgroud)

main.ts

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

bootstrap(App, [disableDeprecatedForms(),provideForms()])
Run Code Online (Sandbox Code Playgroud)

Plunker的例子

也可以看看


Sha*_*ian 7

在app.modules.ts之后

import { NgModule } from '@angular/core'; 
Run Code Online (Sandbox Code Playgroud)

放:

import { FormsModule } from '@angular/forms'; 
Run Code Online (Sandbox Code Playgroud)

然后在

imports: [
 BrowserModule
 ],
Run Code Online (Sandbox Code Playgroud)

插入FormsModule类似于:

imports: [
 BrowserModule,
 FormsModule
 ],
Run Code Online (Sandbox Code Playgroud)


Rob*_*don 5

这发生在我的测试套件中,尽管我已经导入FormsModule到我的组件*.module.ts文件中。

在我的*.component.spec.ts文件中,我需要将FormsModule 添加ReactiveFormsModule到导入列表中configureTestingModule

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

...
TestBed.configureTestingModule({
    imports: [ 
        FormsModule, 
        ReactiveFormsModule,
        ....],
    providers: [MyComponent, ...],
    declarations: [MyComponent],
    schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
Run Code Online (Sandbox Code Playgroud)

这解决了我的情况。