I am creating a patient management system with Angular and TypeScript and using FormControlName
as shown in the following code:
ngOnInit() {
this.formularioPaciente = this.fb.group({
TipoDocumento: ['', Validators.required],
NumDocumento: ['', Validators.required],
urlImg: [''],
PNombre: ['', Validators.required],
SNombre: [''],
PApellido: ['', Validators.required],
SApellido: ['', Validators.required],
Genero: ['', Validators.required],
FechaNacimiento: ['', Validators.required],
Edad: [] ,
EstadoCivil: ['', Validators.required],
RH:['', Validators.required],
EPS: [''],
Departamento: ['', Validators.required],
Municipio: ['', Validators.required],
Zona: ['', Validators.required],
Barrio: ['', Validators.required],
Direccion: [''],
Telefono: [''],
Email: [''],
NomPadre: [''],
NomMadre: ['']
})
}
The point is that to calculate the value of the Age Field I am using the following function using the moments js library:
calcularA(){
this.formularioPaciente.value.FechaNacimiento = moment(new Date(this.formularioPaciente.value.FechaNacimiento));
let fechafin = moment(Date.now());
let diff = fechafin.diff(this.formularioPaciente.value.FechaNacimiento, 'year');
return diff;
}
How can I assign the Value of my function calcularA()
to the field Edad
of my FormControlName
try as follows:
Edad:[this.calcularA()]
but it generates an error that tells me value is undefined, please help me I'm stuck.
You can modify the value of any FormControl using its method
setValue
:or in a more elegant way (this is debatable, but it seems so to me):
So in your case you can do the following: