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

时间:2016-08-17 13:46:33

标签: angularjs

在Angular 2 "Hero tour" tutorial之后,我遇到以下错误:

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: 

我的 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 内容:

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 { }

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'
    };
}

我花时间在stackoverflow上阅读并应用类似问题中提出的解决方案但没有成功。

可能出现什么问题?

解决方案:

使用

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

platformBrowserDynamic().bootstrapModule(AppModule)

而不是:

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

5 个答案:

答案 0 :(得分:4)

您的app.module.ts应如下所示:

add_filter( 'wp_mail_content_type', 'set_content_type' );
function set_content_type( $content_type ) {
    return 'text/html';
}

更新:现在已经过时使用RC6及其他

答案 1 :(得分:2)

RC6 中,它看起来像这样:

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

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

export class AppModule { }

即。已弃用provideFormsdisableDeprecatedForms。查看https://github.com/angular/angular/blob/master/CHANGELOG.md了解详情。

答案 2 :(得分:0)

谢谢你,除非那部分......什么都没发生! import { FormsModule, provideForms, disableDeprecatedForms } from '@angular/forms';

答案 3 :(得分:0)

如果您未根据需要更新app.modules.ts文件,则会发生类似的错误。 那就是添加以下导入

import { FormsModule }   from '@angular/forms';
和FormsModule如下:

imports:      [ BrowserModule, FormsModule ],

答案 4 :(得分:0)

该应用程序正常。但我在测试中遇到了这个错误。你应该像app.module.ts一样在app.component.spec.ts中导入FormsModule来解决这个问题。