0
I have an Angular 8 application and I have two components, a parent and a child and I have an array of objects in the child component and I need to send the array information to the parent component and put that chosen value in a Formgroup
creat-Category.component.ts(parent)
@Component({
selector: 'app-creat-category',
templateUrl: './creat-category.component.html',
styleUrls: ['./creat-category.component.css']
})
export class CreatCategoryComponent implements OnInit {
infoCategory: FormGroup;
loading = false;
constructor(
private formBuilder: FormBuilder,
) { }
ngOnInit(): void {
this.infoCategory = this.formBuilder.group({
nameCategory: [null, Validators.required],
subCategory: [false],
selectSubCategory: [null],
});
console.log(this.infoCategory)
}
creatCategory(){
this.loading = true;
console.log(this.infoCategory.value);
this.loading = false;
}
get c() {
return this.infoCategory.controls;
}
}
creat-Category.component.html(parent)
<div class="container p-5">
<form (submit)="creatCategory()" [formGroup]="infoCategory">
<div class="form-group mb-3">
<label>Nome da categoria</label>
<input formControlName="nameCategory" type="text" class="form-control"/>
<div class="invalid-feedback d-block" *ngIf="c.nameCategory.invalid && (c.nameCategory.dirty || c.nameCategory.touched)">
Favor preencher este campo.
</div>
</div>
<div class="form-group form-check">
<input formControlName="subCategory" type="checkbox" class="form-check-input">
<label class="form-check-label">Sub categoria</label>
</div>
<div *ngIf="infoCategory.value.subCategory">
<app-sub-category></app-sub-category>
</div>
<button [disabled]="!infoCategory.valid" class="btn btn-primary">Cadastrar produto</button>
<div *ngIf="loading" class="spinner-border spinner" role="status"></div>
</form>
</div>
sub-Category.component.ts(child)
@Component({
selector: 'app-sub-category',
templateUrl: './sub-category.component.html',
styleUrls: ['./sub-category.component.css']
})
export class SubCategoryComponent implements OnInit {
subCategory: Object = [
{
"name": "Categoria 1",
},
{
"name": "Categoria 2",
}
];
constructor() { }
ngOnInit(): void {
}
}
sub-Category.component.html(child)
<label>Nome da subcategoria</label>
<div class="form-group mb-3">
<select class="form-control">
<option *ngFor="let s of subCategory" >{{ s.name }}</option>
</select>
</div>
I need to take what was chosen in the select tag and set the value to Formbuilder’s selectSubCategory variable, tried a few things, like @Output and @Input but was not successful.