Angular2和Observables:不能绑定到'ngModel',因为它不是'select'的已知属性 [英] Angular2 and Observables: Can't bind to 'ngModel' since it isn't a known property of 'select'
本文介绍了Angular2和Observables:不能绑定到'ngModel',因为它不是'select'的已知属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
编辑:更新Plunkr: http://plnkr.co/edit/fQ7P9KPjMxb5NAhccYIq?p=preview
Updated Plunkr: http://plnkr.co/edit/fQ7P9KPjMxb5NAhccYIq?p=preview
这部分作品:
<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:
无法绑定到'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
add
'@angular/forms': {
main: 'bundles/forms.umd.js',
defaultExtension: 'js'
},
在 main.ts $ c $从@ angular / forms中添加
in main.ts
add
import {provideForms, disableDeprecatedForms} from '@angular/forms';
bootstrap(App, [disableDeprecatedForms(),provideForms()])
另请参阅
- https://angular.io/docs/ts/latest/cookbook/rc4-to-rc5.html
这篇关于Angular2和Observables:不能绑定到'ngModel',因为它不是'select'的已知属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文