2017-01-21 8 views
0

ImはngModelとの双方向データバインドを試みており、動作しません。 FormsModuleが含まれています。ここ コード:双方向データバインディング - 角2

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 { TestComponent } from './test/test.component'; 

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

test.component.ts:

import { Component, OnInit } from '@angular/core'; 

@Component({ 
    selector: 'app-test', 
    templateUrl: './test.component.html', 
    styleUrls: ['./test.component.css'] 
}) 
export class TestComponent implements OnInit { 

    person = { 
    name: 'David', 
    age: '17' 
    }; 

    constructor() { } 

    ngOnInit() { 
    } 

} 

がtest.component.html:

<form> 
    <input type="text" [(ngModel)]="person.name" /> 
    <input type="text" [(ngModel)]="person.age" /> 
</form> 

{{person.name}}<br /> 
{{person.age}} 

それはなぜです働いていない?

ありがとうございました。

+0

コンソールにエラーがありますか?あなたはkemskyが提案したようにあなたの入力に名前を追加する必要があります –

答えて

1

入力には、name属性を追加する必要があります。

+0

ありがとう、それは今働いています。 –

0

他の方法は、彼が、フォームのどの部分ではありません理解し

[ngModelOptions]="{standalone: true}" 

を追加することです。

関連する問題