2017-02-22 38 views
6

的已知屬性,當我使用的雙向綁定[(ngModel)]我得到此消息角2:模板解析錯誤:不能結合到「ngModel」,因爲它不是「輸入」

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

據我所知,導入FormsModule是想解決這個問題,因爲許多人到這裏。但是,我確實導入了FormsModule,但它沒有幫助,問題仍然存在

當然,我的代碼還有其他問題。你能發光嗎? 這裏是我的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

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>

我在此附上我的源代碼,我使用的角CLI source

回答

2

如果您要使用比您需要添加的反應形式:ReactiveFormsModule

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

這很奇怪,但你的確是答案,在我的示例中,FormsModule也需要在路由模塊中導入,同時它也位於應用程序模塊中。這是記錄在某處嗎?你能給出一個報價嗎? – khoailang

5

你需要,你正在使用它的指令來增加FormsModule進口模塊中:

@NgModule({ 
    declarations:[ 
     Home1Component, 
     Home2Component 
    ], 
    imports: [ 
     RouterModule.forRoot(appRoutes), 
     FormsModule, // <<<=== missing 
    ], 
    exports:[ 
     RouterModule 
    ] 
}) 
相關問題