import { ReactiveFormsModule } from '@angular/forms';
@NgModule({ imports: [ ReactiveFormsModule, // ... ], // ... }) export class AppModule { }
import { Component, OnInit } from '@angular/core'; import { FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'app-my-form',
template:
,
})
export class MyFormComponent implements OnInit {
myForm: FormGroup;
ngOnInit() { this.myForm = new FormGroup({ firstName: new FormControl(), // more form controls here }); }
onSubmit() { console.log(this.myForm.value); } }
new FormGroup({ firstName: new FormControl(), // FormControl 实例名为 'firstName' // ... }); // FormControlName 为 'firstName'
new FormGroup({ firstName: new FormControl(''), // FormControl 实例值为空字符串 // ... });