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

标签 json asynchronous drop-down-menu angular angular2-observables

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

这部分工作:

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

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

无法绑定(bind)到“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());
  }
}

和data.json

[
  {
    "timestamp": 0,
    "label": "l1",
    "value": 1
  },
  {
    "timestamp": 0,
    "label": "l2",
    "value": 2
  },
  {
    "timestamp": 0,
    "label": "l3",
    "value": 3    
  }
]

最佳答案

>= RC.5

需要导入 FormsModule 以使 ngModel 可用

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

<= RC.4

config.js中添加

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

main.ts 添加

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

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

Plunker example

另见

关于json - Angular 2+ 和 Observables : Can't bind to 'ngModel' since it isn't a known property of 'select' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38983766/

相关文章:

php - 表单中下拉框的数据作为 'Array' 发送到数据库

javascript - 使用 JQuery 和 Javascript 获取 json 并解析它的最简单方法?

javascript - 如何从php到html获取php数组中项目的位置整数?

json - 如何定义 JSON 架构中的属性序列?

javascript - 通过 Javascript 加载 CDN 文件

html - 对齐图片下方和旁边的文本

c# - 使用 .NET Core System.Text.Json 序列化/反序列化类层次结构

javascript - 这真的是一个异步 "for-like"循环吗?

node.js - NodeJS 文件系统同步和性能

javascript 下拉菜单 - 选择后转到 url