简体   繁体   English

Angular2 RC5教程:无法绑定到'ngModel',因为它不是'input'的已知属性

[英]Angular2 RC5 tutorial : Can't bind to 'ngModel' since it isn't a known property of 'input'

Following the Angular 2 "Hero tour" tutorial , I encounter the following error: 继Angular 2 “Hero tour”教程之后 ,我遇到以下错误:

Unhandled Promise rejection: Template parse errors:  
Can't bind to 'ngModel' since it isn't a known property of 'input'. ("
    <div>
      <label>name: </label>
      <input [ERROR ->][(ngModel)]="hero.name" placeholder="name">
    </div>
    "): AppComponent@6:13 ; Zone: <root> ; Task: Promise.then ; Value: 

My package.json content is: 我的package.json内容是:

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
    "lite": "lite-server",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings"
  },
  "license": "ISC",
  "dependencies": {
    "@angular/common": "2.0.0-rc.5",
    "@angular/compiler": "2.0.0-rc.5",
    "@angular/core": "2.0.0-rc.5",
    "@angular/forms": "0.3.0",
    "@angular/http": "2.0.0-rc.5",
    "@angular/platform-browser": "2.0.0-rc.5",
    "@angular/platform-browser-dynamic": "2.0.0-rc.5",
    "@angular/router": "3.0.0-rc.1",
    "@angular/router-deprecated": "2.0.0-rc.2",
    "@angular/upgrade": "2.0.0-rc.5",
    "systemjs": "0.19.27",
    "core-js": "^2.4.0",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.6",
    "zone.js": "^0.6.12",
    "angular2": "^2.0.0-beta.17",
    "angular2-in-memory-web-api": "0.0.15",
    "bootstrap": "^3.3.6"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "lite-server": "^2.2.0",
    "typescript": "^1.8.10",
    "typings":"^1.0.4"
  }
}

app.module.ts content: app.module.ts内容:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';

@NgModule({
    imports: [ BrowserModule, FormsModule ],
    declarations: [ AppComponent ],
    bootstrap: [ AppComponent ]
})

export class AppModule { }

And app.component content : 和app.component内容:

import { Component } from '@angular/core';
export class Hero {
    id: number;
    name: string;
}
@Component({
    selector: 'my-app',
    template: `
    <h1>{{title}}</h1>
    <h2>{{hero.name}} details!</h2>
    <div><label>id: </label>{{hero.id}}</div>
    <div>
      <label>name: </label>
      <input [(ngModel)]="hero.name" placeholder="name">
    </div>
    `
})
export class AppComponent {
    title = 'Tour of Heroes';
    hero: Hero = {
        id: 1,
        name: 'Windstorm'
    };
}

I spent time to read and apply solutions proposed in similar question on stackoverflow with no success. 我花时间在stackoverflow上阅读并应用类似问题中提出的解决方案但没有成功。

What could be wrong? 可能有什么不对?

Solution : 方案:

use 使用

//main entry point
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app';

platformBrowserDynamic().bootstrapModule(AppModule)

instead of : 代替 :

import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
bootstrap(AppComponent);

Your app.module.ts should look like this: 你的app.module.ts应如下所示:

 import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule, provideForms, disableDeprecatedForms } from '@angular/forms'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule, FormsModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ], providers: [ disableDeprecatedForms(), provideForms()], }) export class AppModule { } 

UPDATE: this is now obsolete with RC6 and beyond 更新:RC6及更高版本现在已经过时了

In RC6 it looks like this: RC6中它看起来像这样:

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

@NgModule({
    imports: [ ..., FormsModule ],
    declarations: [ AppComponent ],
    bootstrap: [ AppComponent ]
})

export class AppModule { }

Ie provideForms , disableDeprecatedForms are removed as deprecated. 即提供provideFormsdisableDeprecatedForms被删除为已弃用。 Check https://github.com/angular/angular/blob/master/CHANGELOG.md for details. 有关详细信息,请查看https://github.com/angular/angular/blob/master/CHANGELOG.md

Thanks for it, unless that part...nothing going on! 谢谢你,除非那部分......什么都没发生! import { FormsModule, provideForms, disableDeprecatedForms } from '@angular/forms';

A similar error occurs if you don't update the app.modules.ts file as required. 如果您不根据需要更新app.modules.ts文件,则会发生类似的错误。 That is adding the following import 那就是添加以下导入

import { FormsModule }   from '@angular/forms';
and the FormsModule as follows: 和FormsModule如下:

 imports: [ BrowserModule, FormsModule ], 导入:[BrowserModule,FormsModule], 

The app works fine. 该应用程序工作正常。 But I had this error in tests. 但我在测试中遇到了这个错误。 You should import FormsModule in app.component.spec.ts like in app.module.ts to fix this. 你应该在app.monent.ts中的app.component.spec.ts中导入FormsModule来解决这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 2 RC 5 - 无法绑定到&#39;...&#39;,因为它不是&#39;...&#39;的已知属性 - Angular 2 RC 5 - Can't bind to '…' since it isn't a known property of '…' Angular 2 - 无法绑定到&#39;ngModel&#39;,因为它不是&#39;input&#39;的已知属性 - Angular 2 - Can't bind to 'ngModel' since it isn't a known property of 'input' 使用ng2-bootstrap与Angular 2 RC 6 - 无法绑定到无法绑定到[...],因为它不是[...]的已知属性 - Using ng2-bootstrap with Angular 2 RC 6 - can't bind to can't bind to […] since it isn't a known property of […] 无法绑定到“ ngModule”,因为它不是“ input”的已知属性。 在angular 2 asp.net核心中 - Can't bind to 'ngModule' since it isn't a known property of 'input'. in angular 2 asp.net core 无法绑定到 &#39;dtOptions&#39;,因为它不是 &#39;table&#39; 的已知属性。 - Can't bind to 'dtOptions' since it isn't a known property of 'table'. 无法绑定到“ngFor”,因为它不是...在混合 AngularJS 和 Angular 应用程序上的已知属性 - Can't bind to 'ngFor' since it isn't a known property of ... on hybrid AngularJS & Angular application 无法绑定到“ngForOf”,因为它不是 Ionic / Angular 9 中“ion-item”的已知属性 - Can't bind to 'ngForOf' since it isn't a known property of 'ion-item' in Ionic / Angular 9 在 Angular 中使用 cdkDropListData 时出错,无法绑定到“cdkDropListData”,因为它不是“div”的已知属性 - Error when using cdkDropListData in Angular gives me Can't bind to 'cdkDropListData' since it isn't a known property of 'div' 离子图标和“无法绑定到&#39;名称&#39;,因为它不是已知的本机属性”错误 - ionic icon and “Can't bind to 'name' since it isn't a known native property” error |官方指南坏了吗?| 无法绑定到“ ngFor”,因为它不是已知的本机属性 - |Official guide broken?| Can't bind to 'ngFor' since it isn't a known native property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM