Mudanças entre as edições de "Model Driven Forms (Reactive forms) in Angular2"
De Basef
Linha 19: | Linha 19: | ||
field1: new FormControl(), | field1: new FormControl(), | ||
field2: new FormControl() | field2: new FormControl() | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | or | ||
+ | |||
+ | |||
+ | <source lang="javascript"> | ||
+ | import { FormGroup, FormBuilder } from '@angular/forms'; | ||
+ | |||
+ | export class MyComponent { | ||
+ | myForm: FormGroup; | ||
+ | |||
+ | constructor(private fb: FormBuilder) { | ||
+ | this.myForm = fb.group({ | ||
+ | 'field1': '', | ||
+ | 'field2': '' | ||
}); | }); | ||
} | } | ||
Linha 30: | Linha 48: | ||
<input type="text" formControlName="field1" /> | <input type="text" formControlName="field1" /> | ||
<input type="text" formControlName="field2" /> | <input type="text" formControlName="field2" /> | ||
+ | </form> | ||
+ | </source> | ||
+ | |||
+ | or | ||
+ | |||
+ | <source lang="xml"> | ||
+ | <form [formGroup]="myForm"> | ||
+ | <input type="text" [formControl]="myForm.controls['field1']" /> | ||
+ | <input type="text" [formControl]="myForm.controls['field2']" /> | ||
</form> | </form> | ||
</source> | </source> |
Edição das 10h59min de 26 de outubro de 2016
In your Module:
import { ReactiveFormsModule } from '@angular/forms';
Add `ReactiveFormsModule` to `imports` section.
In your Component:
import { FormGroup, FormControl } from '@angular/forms'; export class MyComponent { myForm: FormGroup; constructor() { this.myForm = new FormGroup({ field1: new FormControl(), field2: new FormControl() }); } }
or
import { FormGroup, FormBuilder } from '@angular/forms'; export class MyComponent { myForm: FormGroup; constructor(private fb: FormBuilder) { this.myForm = fb.group({ 'field1': '', 'field2': '' }); } }
In your template:
<form [formGroup]="myForm"> <input type="text" formControlName="field1" /> <input type="text" formControlName="field2" /> </form>
or
<form [formGroup]="myForm"> <input type="text" [formControl]="myForm.controls['field1']" /> <input type="text" [formControl]="myForm.controls['field2']" /> </form>