未处理的承诺拒绝:模板解析错误:无法绑定到' ngModel'因为它不是“输入”

时间:2016-11-10 09:42:15

标签: angular typescript

我总是收到错误无法绑定到' ngModel'因为它不是“输入”的已知属性。如果我尝试使用这样的东西:

<div *ngIf="guide" class="form-group">
    <label for="guideName">Name: </label>
    <input class="form-control" name="guideName" [(ngModel)]="test" required id="guideName">

    <button (click)="saveGuide(guide)"></button>
</div>

我的 app.module.ts 如下所示:

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

import {GuideModule} from "./guide/guide.module";

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

export class AppModule {}

在我的 package.json 中,我有:

"dependencies": {
    ...
    "@angular/forms": "^2.1.2",
    ...
}

我的 systemjs.config.ts

(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'npm:': 'node_modules/'
        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder
            app: 'app',
            // angular bundles
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
            "@angular/forms": "node_modules/@angular/forms/bundles/forms.umd.js",
            '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
            '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
            // other libraries
            'rxjs': 'npm:rxjs',
            'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                main: './main.js',
                defaultExtension: 'js'
            },
            rxjs: {
                defaultExtension: 'js'
            }
        }
    });
})(this);

1 个答案:

答案 0 :(得分:3)

您收到此错误是因为guide-details.component.ts不属于您导入AppModule的{​​{1}}的一部分(FormsModule指令是NgModel的一部分)。您必须在声明FormsModule的模块中导入FormsModule,或将guide-details.component.ts移至guide-details.component.ts的声明。