Angularは9月15日に最終版をリリースしました。Angular 1とは異なりngModel、Angular 2で双方向のデータバインディングにディレクティブを使用できますが、[(ngModel)](ボックス構文のBanana)のように少し異なる方法で記述する必要があります。ほとんどすべてのangular2コアディレクティブはkebab-case現在サポートしていませんcamelCase。代わりにを使用する必要があります。
今ngModelまでディレクティブ属しFormsModuleなぜあなたがすべきだと、から内部モジュールのメタデータオプション(NgModule)。その後、ページ内でディレクティブを使用できます。importFormsModule@angular/formsimportsAppModulengModel
app / app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `<h1>My First Angular 2 App</h1>
<input type="text" [(ngModel)]="myModel"/>
{{myModel}}
`
})
export class AppComponent {
myModel: any;
}
app / app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, FormsModule ], //< added FormsModule here
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
app / main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);
デモプランカー