I'm having trouble populating my entity data on my reactive form.
I can get the data, but I do not know how or when is the best time to fill out my form with these values.
this is my form:
import { Component, OnInit, Input } from '@angular/core';
import { FormArray, FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { Empresa } from '../empresa.model';
import { HttpClient } from '@angular/common/http';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-empresa-detalhe',
templateUrl: './empresa-detalhe.component.html',
styleUrls: ['./empresa-detalhe.component.scss']
})
export class EmpresaDetalheComponent implements OnInit {
empresaForm: FormGroup;
nomeControl: FormControl;
statusControl: FormControl;
empresa: Empresa = {};
constructor(private route: ActivatedRoute, private http: HttpClient, private fb: FormBuilder) {}
ngOnInit() {
this.getEmpresa(this.route.snapshot.params['nome']);
this.createForm();
}
createForm() {
this.nomeControl = new FormControl('', [
Validators.required,
Validators.minLength(3),
Validators.maxLength(10)
]);
this.statusControl = new FormControl('', [
Validators.required,
Validators.minLength(3),
Validators.maxLength(10)
]);
this.empresaForm = this.fb.group({
nome: this.nomeControl,
status: this.statusControl
});
}
onSubmit() {
const empresa = this.empresaForm.value as Empresa;
console.log('Empresa: ', empresa);
}
getEmpresa(nome) {
this.http.get('http://localhost:8080/app-rest/api/empresasAereas/nome/' + nome).subscribe(data => {
this.empresa = data;
console.log(this.empresa); // this return my data with values
});
}
}
my getEmpresa return my empresa model, What is the best way to fill my form data with my entity data?