Mudanças entre as edições de "Model Driven Forms (Reactive forms) in Angular2"
De Basef
(7 revisões intermediárias pelo mesmo usuário não estão sendo mostradas) | |||
Linha 1: | Linha 1: | ||
− | === In your Module: === | + | A complete guide to reactive forms: [http://blog.thoughtram.io/angular/2016/06/22/model-driven-forms-in-angular-2.html Thoughtram - Model Driven Forms in Angular 2] |
+ | |||
+ | === 1) In your Module: === | ||
<source lang="javascript"> | <source lang="javascript"> | ||
Linha 7: | Linha 9: | ||
Add `ReactiveFormsModule` to `imports` section. | Add `ReactiveFormsModule` to `imports` section. | ||
− | === In your Component: === | + | === 2) In your Component: === |
<source lang="javascript"> | <source lang="javascript"> | ||
Linha 27: | Linha 29: | ||
<source lang="javascript"> | <source lang="javascript"> | ||
− | import { FormGroup, FormBuilder } from '@angular/forms'; | + | import { FormGroup, FormBuilder, Validators } from '@angular/forms'; |
export class MyComponent { | export class MyComponent { | ||
Linha 34: | Linha 36: | ||
constructor(private fb: FormBuilder) { | constructor(private fb: FormBuilder) { | ||
this.myForm = fb.group({ | this.myForm = fb.group({ | ||
− | 'field1': '', | + | 'field1': ['', Validators.required], |
− | 'field2': '' | + | 'field2': [''] |
}); | }); | ||
} | } | ||
Linha 41: | Linha 43: | ||
</source> | </source> | ||
− | === In your template: === | + | === 3) In your template: === |
+ | |||
+ | These are similar to ngModel of Angular1: | ||
<source lang="xml"> | <source lang="xml"> | ||
+ | |||
<form [formGroup]="myForm"> | <form [formGroup]="myForm"> | ||
<input type="text" formControlName="field1" /> | <input type="text" formControlName="field1" /> | ||
Linha 50: | Linha 55: | ||
</source> | </source> | ||
− | + | [[Category: Angular]] | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Edição atual tal como às 10h39min de 27 de janeiro de 2020
A complete guide to reactive forms: Thoughtram - Model Driven Forms in Angular 2
1) In your Module:
import { ReactiveFormsModule } from '@angular/forms';
Add `ReactiveFormsModule` to `imports` section.
2) 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, Validators } from '@angular/forms'; export class MyComponent { myForm: FormGroup; constructor(private fb: FormBuilder) { this.myForm = fb.group({ 'field1': ['', Validators.required], 'field2': [''] }); } }
3) In your template:
These are similar to ngModel of Angular1:
<form [formGroup]="myForm"> <input type="text" formControlName="field1" /> <input type="text" formControlName="field2" /> </form>