Angular 2 反應形式(又稱模型驅動形式)
此示例使用 Angular 2.0.0 Final Release
註冊用 form.component.ts
import { FormGroup,
FormControl,
FormBuilder,
Validators } from '@angular/forms';
@Component({
templateUrl: "./registration-form.html"
})
export class ExampleComponent {
constructor(private _fb: FormBuilder) { }
exampleForm = this._fb.group({
name: ['DefaultValue', [<any>Validators.required, <any>Validators.minLength(2)]],
email: ['default@defa.ult', [<any>Validators.required, <any>Validators.minLength(2)]]
})
註冊用 form.html
<form [formGroup]="exampleForm" novalidate (ngSubmit)="submit(exampleForm)">
<label>Name: </label>
<input type="text" formControlName="name"/>
<label>Email: </label>
<input type="email" formControlName="email"/>
<button type="submit">Submit</button>
</form>