Getting Started

Opt-In APIs

Before we dive into any of the form features, we need to do a little bit of housekeeping.We need to bootstrap our application using the FormsModule or ReactiveFormsModule.

  1. import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'
  2. import { FormsModule } from '@angular/forms';
  3. import { AppComponent } from './components'
  4. @NgModule({
  5. imports: [
  6. BrowserModule,
  7. FormsModule,
  8. ],
  9. declarations: [AppComponent],
  10. bootstrap: [AppComponent]
  11. })
  12. export class AppModule {
  13. }
  14. platformBrowserDynamic().bootstrapModule(AppModule)

Input Labeling

Most of the form examples use the following HTML5 style for labeling inputs:

  1. <label for="name">Name</label>
  2. <input type="text" name="username" id="name">

Angular also supports the alternate HTML5 style, which precludes the necessity of ids on <input>s:

  1. <label>
  2. Name
  3. <input type="text" name="username">
  4. </label>

原文: https://angular-2-training-book.rangle.io/handout/forms/getting-started.html