Open jselesan opened 7 years ago
Hi @jselesan,
Try the .setValue()
form control method.
In HTML:
<ng-select [items]="items" (selected)="itemSelected($event)"></ng-select>
In TypeScript:
itemSelected(item) {
this.form.controls['formControlName'].setValue(item.id);
}
I hope it helps 🙂
@VitorHFLopes this solution throws the following error for me
TypeError: selectedItems.map is not a function at SelectComponent.set [as active] (select.js:83)
The setValue
function expects an array so it should be
this.form.controls['formControlName'].setValue([item.id]);
@showerbeer I couldn't reproduce the error :(
That is an item from my formControls object:
formControlName: ['', Validators.required]
I tried to set a number and a string with no errors
Hi there. I'm using ng2-select with reactive forms through "formControlName" and when I get the value of my form, the field value related to the ng2-select is an array of SelectItem. My question is, is there a way to get the "id" of the selected item as the value of the formControl?