角2:模板解析错误:由于它不是'input'的已知属性,因此无法绑定到'ngModel' [英] Angular 2: Template parse errors: Can't bind to 'ngModel' since it isn't a known property of 'input'

查看:88
本文介绍了角2:模板解析错误:由于它不是'input'的已知属性,因此无法绑定到'ngModel'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用双向绑定[[ngModel)]

I got this message when I used the two-way binding [(ngModel)]

Template parse errors: Can't bind to 'ngModel' since it isn't a known property of 'input'.

我了解到,导入FormsModule可以解决此问题,因为很多人都来了.但是,我确实导入了FormsModule,但没有帮助,问题仍然存在

I understand that importing the FormsModule is suppose to fix this issue as many people got here. However, I did had the FormsModule imported but it doesn't help, issue still be there

我的代码肯定有其他问题.你能说明一点吗?这是我的app.module.ts

Definitely there is something else get wrong with my code. Can you shed a light. Here is my app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule, Routes } from '@angular/router';

import { ValidationModule } from './validation/validation.module';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app.module.routing';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    ValidationModule,
    AppRoutingModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { 

}

这是我的app.routing.module.ts

here is my app.routing.module.ts

import { NgModule } from '@angular/core'
import { RouterModule, Routes } from '@angular/router'

import { Home1Component } from './home1.component';
import { Home2Component } from './home2.component';

const appRoutes = [
  { path: 'home1', component: Home1Component },
  { path: 'home2', component: Home2Component },
  { path: 'validation', loadChildren: './validation/validation.module#ValidationModule'}
];

@NgModule({
    declarations:[
        Home1Component,
        Home2Component
    ],
    imports: [
        RouterModule.forRoot(appRoutes)
    ],
    exports:[
        RouterModule
    ]
})

export class AppRoutingModule {

}

这是我的html

<h1> home 1 </h1>
<form>
    <input [(ngModel)]="currentHero.name">
    <button type="button" (click)="onOkClicked()">Ok</button>
</form>

我在此处附加我的源代码,我正在使用angular-cli

I attach my source code here, I'm using angular-cli source

推荐答案

如果要使用反应形式,则需要添加:ReactiveFormsModule

if you want to use reactive forms than you need to add : ReactiveFormsModule

@NgModule({
    declarations:[
        Home1Component,
        Home2Component
    ],
    imports: [
        RouterModule.forRoot(appRoutes),
        ReactiveFormsModule, 
    ],
    exports:[
        RouterModule
    ]
})

这篇关于角2:模板解析错误:由于它不是'input'的已知属性,因此无法绑定到'ngModel'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆