I am using <ion-select>
for drop-down list and it working as expected but what I want here is Index of selected option from <ion-select>
I have used <ion-select>
and change event like below:
<ion-select class="myCustomSelect" [(ngModel)]="selectedProductDD" interface="popover" (ionChange)="onSelectChange($event)">
<ion-option *ngFor="let selectedProduct of productArray" [value] = selectedProduct.pid>{{ selectedProduct.pid }} </ion-option>
</ion-select>
Change Event :
onSelectChange(selectedValue: any) {
//Here I want Index also.
//Currently I am getting selected Value.
}
let me know if any can help me on this!
Thanks in advance.
In your *ngFor
you can also define a variable to receive que index
<ion-select class="myCustomSelect" [(ngModel)]="selectedProductDD" interface="popover" (ionChange)="onSelectChange($event)">
<!-- you can also have your index by declaring a variable in ngFor that'll receive the index -->
<ion-option *ngFor="let selectedProduct of productArray; let i = index" [value] = selectedProduct.pid>{{ selectedProduct.pid }} </ion-option>
</ion-select>
The case here is that your declared index is inside your ion-select
so it's not available for onSelectChange
method. So declare a property in your .TS file
public myIndex: number = 0;
And every time a user select an option the property will receive the index
<!-- on selecting an option your myIndex will receive the current selected option index -->
<ion-option *ngFor="let selectedProduct of productArray; let i = index" [value]=selectedProduct.pid (ionSelect)="myIndex = i">{{ selectedProduct.pid }}</ion-option>
Then you can use it in your onSelectChange()
onSelectChange(selectedValue: any) {
let index = this.myIndex;
}
There's another options but this is the most Angular way of doing this. Hope this helps.