How could I create a multiple select type element which would push an object to a formarray on being clicked?
I want the final JSON object to look like this:
{
business_id: [ {
name: "one"
},]
}
Here is my .ts file
businesses = [
{name: "one", id: 0},
{name: "two", id: 1}
]
ngOnInit(): void {
this.leadGenerateForm = new FormGroup({
'business_id': new FormArray([])
})
}
onClickBusiness(){
const group = new FormGroup({
'name': new FormControl(null)
});
(<FormArray>this.leadGenerateForm.get('business_id')).push(group);
};
Here is the HTML template
<form formGroupName="leadGenerateForm">
<div formArrayName="business_id">
<select multiple (click)="onClickBusiness()">
<div *ngFor="let businessControl of leadGenerateForm.get('business_id')['controls']; let i=index">
<options [formControlName]="i">{{businessControl.name}}</options>
</div>
</select>
</div>
</form>