¡Hola, Habr! Presento a su atención la traducción del artículo "Convertir en formas angulares fuertemente tipificadas en un minuto" de Ajay Ojha.
En este artículo, aprenderemos cómo convertir una forma reactiva en una forma fuertemente tipada sin cambiar la definición de la clase.
TypeScript . , Angular . Angular . - «any», , ..
, Angular. , , . «», .
frontend- , . , , , , , - .
:
- ;
- valueChanges statusChanges FormControl ;
- FormGroup FormArray - .
(FormGroup, FormControl FormArray), . , :
- FormBuilder FormControl, FormGroup FormArray;
- ;
- , FormGroup , @angular/forms.
, .
? ?
, .
?
@rxweb/types. , , - .
, FormControl FormGroup FormArray. FormGroup.
export class AppComponent implements OnInit {
formGroup: FormGroup;
formBuilder: FormBuilder;
constructor(formBuilder: FormBuilder) {
this.formBuilder = formBuilder;
}
ngOnInit() {
this.formGroup = this.formBuilder.group({
firstName: ['', [Validators.required]],
address: this.formBuilder.group({
countryName: ["", Validators.required]
}),
skills: this.formBuilder.array([
this.formBuilder.group({
name: ["", Validators.required]
})
])
});
}
}
.
1.
npm install @rxweb/types
2.
. :
User >> firstName, address, skills; Address >> countryName; Skill >> name.
3.
:
- IFormGroup: API FormGroup;
- IFormBuilder: FormGroup, FormControl FormArray.
:
import { IFormGroup, IFormBuilder } from “@rxweb/types”;
4.
:
- FormGroup IFormGroup;
- FormBuilder IFormBuilder;
-
group<User>
,group<Address>
array<Skill>
.
, , .
:
formGroup: IformGroup<User>;
formBuilder: IformBuilder;
ngOnInit() {
this.formGroup = this.formBuilder.group<User>({
firstName: ['', [Validators.required]],
address: this.formBuilder.group<Address>({
countryName: ["", Validators.required]
}),
skills: this.formBuilder.array<Skill>([
this.formBuilder.group({
name: ["", Validators.required]
})
])
});
}
, .
/,
, , . , , , , , - , «New Way to Validate the Angular Reactive Form».