Angular Javascript return the value of dynamic options on select change

Viewed 33

I have a select input and I am using *ngFor on the options to let the user select from an dynamic array I am getting from the database

<select formControlName="destination_id" (change)="onSelectDestination($any($event.target).value)"
    <option *ngFor="let destination of destinations" [ngValue]="destination.id" class="p-3">
        {{ destination.name }}
    </option>
</select>

The function

onSelectDestination(id: any) {
  console.log(id);
}

The problem here is that returns the index of the item and the value

for example: if I chose the third item in the select input it returns this 2: 27

2: is the index and 27 is the id I want

I used substr(3, 2) but it's making problems when the index or the id is bigger than two or three letters

What is the best practice to do this?

1 Answers

Can you try to replace [value] in place of [ngValue]

value is refer to string and ngValue reference to object which includes the index + value.

Related