2017-06-14 11 views
2

これは一般的な問題であると思われますが、ソリューションはすべてFormsModuleをインポートすることを提案しており、これが行われています。角4 "ngModelにはバインドできません。これは 'input'のプロパティではないためです。

エラーがある:それは「入力」の既知のプロパティではありませんので、

は「ngModel」にバインドできません。 (」

<label for="city">City</label> 
<input type="text" id="city" [ERROR ->][(ngModel)]="chosenCity"> 
<input type="button" value="Get Weather" (click)="getWeather(chosenCity)" "): 

テンプレートと部品コード次のとおり

import { Component } from '@angular/core'; 
 
import { Http } from '@angular/http'; 
 

 
@Component({ 
 
    selector: 'weather', 
 
    templateUrl: './weather.component.html' 
 
}) 
 
export class WeatherComponent { 
 
    public weather: Weather; 
 

 
    constructor(private http: Http) { 
 
    } 
 

 
    public getWeather(chosenCity: string): void { 
 
     this.http.get('/api/weather/city/' + chosenCity).subscribe(result => { 
 
      this.weather = result.json(); 
 
     }); 
 
    } 
 
} 
 

 
interface Weather { 
 
    temp: string; 
 
    summary: string; 
 
    city: string; 
 
}
<h1>Weather check</h1> 
 

 
<label for="city">City</label> 
 
<input type="text" id="city" [(ngModel)]="chosenCity"> 
 
<input type="button" value="Get Weather" (click)="getWeather(chosenCity)" /> 
 

 

 
<div *ngIf="weather"> 
 
    <h3>Weather for {{weather.city}}</h3> 
 

 
    <table class="table table-bordered table-striped"> 
 
     <thead> 
 
      <tr> 
 
       <th>Temp</th> 
 
       <th>Summary</th> 
 
      </tr> 
 
     </thead> 
 
     <tbody> 
 
      <tr> 
 
       <td>{{weather.temp}}</td> 
 
       <td>{{weather.summary}}</td> 
 
      </tr> 
 
     </tbody> 
 
    </table> 
 
</div>

app.module.shared.tsは、以下:

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

 
import { AppComponent } from './components/app/app.component' 
 
import { NavMenuComponent } from './components/navmenu/navmenu.component'; 
 
import { HomeComponent } from './components/home/home.component'; 
 
import { FetchDataComponent } from './components/fetchdata/fetchdata.component'; 
 
import { CounterComponent } from './components/counter/counter.component'; 
 
import { HelloWorldComponent } from './components/helloworld/helloworld.component'; 
 
import { WeatherComponent } from './components/weather/weather.component'; 
 

 
export const sharedConfig: NgModule = { 
 
    bootstrap: [ AppComponent ], 
 
    declarations: [ 
 
     AppComponent, 
 
     NavMenuComponent, 
 
     CounterComponent, 
 
     FetchDataComponent, 
 
     HomeComponent, 
 
     HelloWorldComponent, 
 
     WeatherComponent 
 
    ], 
 
    imports: [ 
 
     RouterModule.forRoot([ 
 
      { path: '', redirectTo: 'home', pathMatch: 'full' }, 
 
      { path: 'home', component: HomeComponent }, 
 
      { path: 'counter', component: CounterComponent }, 
 
      { path: 'fetch-data', component: FetchDataComponent }, 
 
      { path: 'hello', component: HelloWorldComponent }, 
 
      { path: 'weather', component: WeatherComponent }, 
 
      { path: '**', redirectTo: 'home' } 
 
     ]) 
 
    ] 
 
};

そしてapp.module.client.tsファイルは、次のとおりです。誰もが私が間違っているのものを私に指摘することができる場合、私は多くの義務がある

import { NgModule } from '@angular/core'; 
 
import { BrowserModule } from '@angular/platform-browser'; 
 
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; 
 
import { HttpModule } from '@angular/http'; 
 
import { sharedConfig } from './app.module.shared'; 
 
@NgModule({ 
 
    bootstrap: sharedConfig.bootstrap, 
 
    declarations: sharedConfig.declarations, 
 
    imports: [ 
 
     BrowserModule, 
 
     HttpModule, 
 
     FormsModule, 
 
     ReactiveFormsModule, 
 
     ...sharedConfig.imports 
 
    ], 
 
    providers: [ 
 
     { provide: 'ORIGIN_URL', useValue: location.origin } 
 
    ] 
 
}) 
 
export class AppModule { 
 
}

が、すべての記事私は、問題がFormsModuleをインポートすることで解決できることを示しています。

+0

具体的には、FormsModuleをインポートするのが一般的な答えですが、コードではインポートされていることがわかりました。 –

+0

ap.moduleの@ angular/commonからCommonModuleをインポートし、試してみてください。 –

+0

@MartinHortonあなたの 'input'タグに名前属性を追加する必要があります –

答えて

4

あなたのapp.module.shared.tsは次のようにする必要がありますあなたのapp.module.shared.ts

FormsModule, ReactiveFormsModuleを追加する必要があります。

import { NgModule } from '@angular/core'; 
import { RouterModule } from '@angular/router'; 
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; 
import { AppComponent } from './components/app/app.component' 
import { NavMenuComponent } from './components/navmenu/navmenu.component'; 
import { HomeComponent } from './components/home/home.component'; 
import { FetchDataComponent } from './components/fetchdata/fetchdata.component'; 
import { CounterComponent } from './components/counter/counter.component'; 
import { HelloWorldComponent } from './components/helloworld/helloworld.component'; 
import { WeatherComponent } from './components/weather/weather.component'; 

export const sharedConfig: NgModule = { 
    bootstrap: [ AppComponent ], 
    declarations: [ 
     AppComponent, 
     NavMenuComponent, 
     CounterComponent, 
     FetchDataComponent, 
     HomeComponent, 
     HelloWorldComponent, 
     WeatherComponent 
    ], 
    imports: [ 
     FormsModule, 
     ReactiveFormsModule 
     RouterModule.forRoot([ 
      { path: '', redirectTo: 'home', pathMatch: 'full' }, 
      { path: 'home', component: HomeComponent }, 
      { path: 'counter', component: CounterComponent }, 
      { path: 'fetch-data', component: FetchDataComponent }, 
      { path: 'hello', component: HelloWorldComponent }, 
      { path: 'weather', component: WeatherComponent }, 
      { path: '**', redirectTo: 'home' } 
     ]) 
    ] 
}; 
+0

それはうまくいったが、私が持っていたものとあなたが提案したものの違いを理解できない。とにかく、ありがとうございました! –

+0

@MartinHortonあなたが遅延読み込みメソッドを使用して新しいモジュールを作成するので、コンポーネントを追加するモジュール内のすべてのものをインポートする必要があります。ありがとう –

+0

詳細についてはhttps://angular.io/guide/ngmodule#!#lazy-load –

関連する問題