0
I have a component called detail-paciente
in which it contains a form with several properties, one of which is a Formarray. I want to use the same form as this component in a child component, as I could "inherit" the information from form
to then use Formarray in the child component acompanhantes
that I created?
component detail-paciente
public validation(): void {
this.form = this.fb.group({
pacienteId: ['', Validators.required],
nomePaciente: ['', [Validators.required, Validators.minLength(9), Validators.maxLength(50)]],
dataNascimento: ['', Validators.required],
dataRegistro: [''],
rg: [''],
sexo: ['', Validators.required],
acompanhantes: this.fb.array([]),
});
}
As a test I created the Formarray of companions within the Component detail-paciente
and it works
correctly.
get acompanhantes(): FormArray {
return this.form.get('acompanhantes') as FormArray;
}
AddAcompanhante(): void {
this.acompanhantes.push(this.CreateAcompanhante({ acompanhanteId: 0 } as Acompanhante));
}
CreateAcompanhante(acompanhante: Acompanhante): FormGroup {
return this.fb.group({
acompanhanteId: [acompanhante.acompanhanteId, Validators.required],
nomeAcompanhante: [acompanhante.nomeAcompanhante, Validators.required],
dataNascimento: [acompanhante.dataNascimento, Validators.required],
sexo: [acompanhante.sexo, Validators.required],
pacienteId: [],
});
}
<div [formGroup]="form" class="col">
<div formArrayName="acompanhantes" *ngFor="let acompanhante of acompanhantes.controls; let i=index">
<h3>Test</h3>
<fieldset [formGroupName]="i" class="form-group">
...
How I use the above code on a brother component?
You can pass the form through a @Input() to the child component
– Bruno Cunha
And if I were to move from the child component to another child component it would be with @Input() tbm?
– MenesesH3
Yes, you can do it that way. But when you start passing an attribute across many layers (parent -> child -> grandson), it’s best to create a Service to share data between components.
– Bruno Cunha
Well, I’m a beginner in angular, and I still wondered how to use Input/Output using sister components, I’m using <router-outlet>, how do I define the ownership of the main form(form) on the sister route? ex: [formAcompanhante]=form
– MenesesH3